More About PHP OOP - Classes within Classes

2019-07-21 03:06发布

I have been told that a class cannot be defined within a class in PHP. However, in my own example this seems to work which has me confused:

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 prints out

Hello World - A Callback

Question: How do I define $missing_variable such that I can get it where I need it?


In case anyone in the future has a similar problem, however unlikely that may be, I want to link to the codepad from below that shows the $missing_variable echo'd from outside the classes:

http://codepad.org/tRk0XWG7

Thanks again everyone.


Note: This is a follow up.

2条回答
贪生不怕死
2楼-- · 2019-07-21 03:39

You can declare a class within a function. That's known as conditional declaration, i.e. only if the function is called will the class be declared. It doesn't make much of a difference then whether you include a file with the class declaration or if you type out the code inside the function.

This does not mean however that the classes share any sort of scope or data. Only the declaration is conditionally nested, it still has the same functionality and scope as explained before.

Your confusion about the callback can be explained by the same thing. When class_1::function_1 is executed the first time, the function callback_function is being defined. This is a regular global function that can be called from anywhere. It's not bound to the class in any way. You will also notice that you cannot execute class_1::function_1 a second time, PHP will complain that callback_function already exists when you're trying to declare it again.

As for the comment in the source code //How do I declare this variable so that it is available where I need it?: You don't. That variable is a local variable inside a function. It's only in scope inside the function. You can return its value from the function like any other return value if you want to. (You could make it global, but for the love of god don't!) If you need that value somewhere else, don't declare it as a variable inside a function, because only the function can access it then.

查看更多
再贱就再见
3楼-- · 2019-07-21 03:48

You would return $missing_variable in a few places. See below. (This isn't the only way to do it, 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;
查看更多
登录 后发表回答