More on PHP OOP - Classes in Classes

I was told that a class cannot be defined in a class in PHP. However, in my own example, this seems to work, which confuses me:

class_test.php:

require('class_1.php'); new class_1 //Need $missing_variable here. 

class_1.php

 class class_1{ public function function_1(){ function callback_function(){ echo "A Callback"; $missing_variable = "Where Did I Go?"; } require('class_2.php'); new class_2('callback_function'); } public function __construct(){ $this->function_1(); } } 

class_2.php

 class class_2{ public function __construct($callback){ echo "Hello World - "; call_user_func($callback); } } 

Loading class_test.php displays

 Hello World - A Callback 

Question : How to determine $missing_variable so that I can get it where I need it?


In case someone has a similar problem in the future, whatever the possibility that this is possible, I want to link to the codec below, which shows $ missing_variable echo'd from outside the classes:

http://codepad.org/tRk0XWG7

Thanks again to everyone.


Note. This is a sequel .

+2
source share
2 answers

You will return $missing_variable in several places. See below. (This is not the only way to do this, mind you)

http://codepad.org/tf08Vgdx

 <? class class_2{ public function __construct($callback){ echo "Hello World - "; $missing = $callback(); $this->missing = $missing; } } class class_1{ public function function_1(){ function callback_function(){ echo "A Callback. "; $missing_variable = "Where Did I Go?"; return $missing_variable; } $class2 = new class_2('callback_function'); return $class2->missing; } public function __construct(){ $this->missing = $this->function_1(); } } $class = new class_1(); echo $class->missing; 
+1
source

You can declare a class inside a function. This is called a conditional declaration, i.e. Only when the function is called will the class be declared. It doesn't really matter whether you include class declaration file or if you select the code inside the function.

This does not mean, however, that classes have a common view or data. Only an ad is conditionally nested, it still has the same functionality and scope as previously described .

Your confusion about the callback can be explained by the same thing. When class_1::function_1 is executed for the first time, function callback_function defined. This is a regular global function that can be called from anywhere. This has nothing to do with the class. You will also notice that the second time you cannot execute class_1::function_1 , PHP will complain that the callback_function already exists when you try to declare it again.

Regarding the comment in the source code //How do I declare this variable so that it is available where I need it? : you do not do this. This variable is a local variable inside the function. This is only in the area inside the function. You can return its value from the function, like any other return value if you want. (You can make it global , but not for the love of God!) If you need this value somewhere else, do not declare it as a variable inside the function, because only the function can access it.

+3
source

Source: https://habr.com/ru/post/1447160/


All Articles