<?phpclass PageShow {
public $currentpage;
public function __construct($pageobj){$this->currentpage = $pageobj;}
public function show(){echo $this->currentpage->name;$state = ($this->currentpage->status == 1) ? 'Active' : 'Inactive';echo 'This is ' . $state . ' page';}}
$book = new stdClass;$book->title = "Harry Potter and the Prisoner of Azkaban";$book->author = "J. K. Rowling";$book->publisher = "Arthur A. Levine Books";$book->amazon_link = "http://www.amazon.com/dp/0439136369/";
class Example {
private $options;
public function __construct(Array $setup){// casting Array to stdClass object$this->options = (object) $setup;
// access stdClass object in oop style - here transform data in OOP style using some custom method or something...echo $this->options->{'name'}; // ->{'key'}echo $this->options->surname; // ->key}
}
$ob1 = new Example(["name" => "John", "surname" => "Doe"]);
//example 1$data=array('k1'=>'v1' , 'k2'=>'v2',....);
//example 2//creating an empty class is faster than instances an stdClassclass data={}$data=new data();$data->k1='v1';$data->k2='v2';
$a = [1, 2, 3]; // this is an array$b = ['one' => 1, 'two' => 2]; // this is an associate array (aka hash)$c = ['a' => $a, 'b' => $b]; // this is also an associate array (aka hash)
$a = [1, 2, 3]; // this is an array$b = (object) ['one' => 1, 'two' => 2]; // this makes it an stdClass$c = ['a' => $a, 'b' => $b]; // this is also an associate array (aka hash)
$c = json_decode('{"a": [1,2,3], "b": {}}', true); //true to deocde as array// $c is now ['a' => [1,2,3], 'b' => []] in PHP// if you json_encode($c) again your data is now corrupted