I have a class. Lets call it classA. class classA { public $smarty; __construct () { $this->smarty = new Smarty(); //Lets just assume that I've done all the necessary includes etc for smarty } } $classA = new classA; PHP: Basically what I'd like is Smarty to be acessible, using: $classA->smarty eg $classA->smarty->assign('blah','blah'); I find that works up to a point, it will work if I do a $this->smarty->assign(); but it wont work if I do a $classA->smarty->assign(); Am I messing up somewhere? Any ideas?
well, I suppose "assign" won't work because it's a function ... and functions need to be followed by () in order to work properly ..
You have syntax error, the constructor is a function: public function __construct( ) {} PHP: <?php class classA { public $smarty; public function __construct () { $this->smarty = new Smarty(); //Lets just assume that I've done all the necessary includes etc for smarty } } class Smarty { public function lol( ) { echo "LOL"; } } $classA = new classA; $classA->smarty->lol( ); ?> PHP:
Yes, it is as simple as that. The reason yours didn't worked is because you forgot to add the "function" keyword before "__construct". Therefore, whenever you instantiate $classA, the constructor doesn't get called so object $smarty doesn't get instantiated. To put it short, you made a typo.