You should not embed such classes. It doesn't even have to be. First I suggest running the code. There are several online tools for testing small pieces of PHP code, such as this .
To run the code, as you might expect, it should look like this:
class second{ public function widgets(){ $a_variable = $a_value; } } class first{ public $second; public function __construct() { $this->second = new second; } } $first = new first;
A variable starting with $ [az] + is local to the function. A property starting with $ this โ [az] + (where [az] is 1 or more letters) is part of the object.
There is documentation on php.net, which describes the specifics of objects in php here .
If I initialize $ a_variable as $ a_variable, it is only available inside the function, right?
Yes, right. It starts with $ [az]. Not entirely true if you use the global , but that is discouraged.
If I initialize $ a_varialbe as $ this-> a_variable, it is only available inside the second class, right?
Yes, but first you have to announce it. You can do this with public $variable , protected $variable or private $variable . public means that the property can be accessed from the outside, while private means that only the class itself can access it. protected is private external, but public for classes that extend from your class.
( public / private / public became available in PHP 5. In PHP 4, you would use var $variable , which will be public in PHP 5 by default)
Is it possible to initialize $ a_variable as $ first-> second-> a_variable?
You can arbitrarily initialize class variables without declaring them, but this is not what you should be doing.
If so, how can I call it at # 1, # 2 and # 3?
You cannot call the code there (in your example). The code must be inside a function or in a global context (outside the class definition).