I just don’t understand why i can’t call an object like this.
<?php
$obj = (object) array (
"happy" => " :) ",
"sad" => " :( "
);
class MyClass
{
function __construct () {}
function something ()
{
echo "Hello World\n";
echo $obj->sad;
}
}
$class = new MyClass();
echo $obj->happy;
$class->something();
the output seem like
:) Hello World
and it’s not what I expect. i.e.
:) Hello World :(
how can I make this work??
EDIT:
This is in what I will implement this example. passing objects from the global scope to a model
That will be a way of doing it.
obj is not in scope here, you have to either make it global, or pass it.
Pass it to constructor and save instance..as here
But you will have to access $obj as $this->obj from all functions