在 PHP 中的另一个类中调用一个类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1468616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:44:14  来源:igfitidea点击:

Call a class inside another class in PHP

phpclassabstract-class

提问by Supernovah

Hey there I'm wondering how this is done as when I try the following code inside a function of a class it produces some php error which I can't catch

嘿,我想知道这是如何完成的,因为当我在类的函数中尝试以下代码时,它会产生一些我无法捕获的 php 错误

public $tasks;
$this->tasks = new tasks($this);
$this->tasks->test();

I don't know why the initiation of the class requires $this as a parameter either :S

我不知道为什么课程的启动需要 $this 作为参数:S

thanks

谢谢

class admin
{
    function validate()
    {
        if(!$_SESSION['level']==7){
            barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        }else{
            **public $tasks;** // The line causing the problem
            $this->tasks = new tasks(); // Get rid of $this->
            $this->tasks->test(); // Get rid of $this->
            $this->showPanel();
        }
    }
}
class tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new admin();
$admin->validate();

回答by Lance Kidwell

You can't declare the public $tasks inside your class's method (function.) If you don't need to use the tasks object outside of that method, you can just do:

您不能在类的方法(函数)中声明公共 $tasks。如果您不需要在该方法之外使用 tasks 对象,您可以这样做:

$tasks = new Tasks($this);
$tasks->test();

You only need to use the "$this->" when your using a variable that you want to be available throughout the class.

当您使用希望在整个类中可用的变量时,您只需要使用“$this->”。

Your two options:

你的两个选择:

class Foo
{
    public $tasks;

    function doStuff()
    {
        $this->tasks = new Tasks();
        $this->tasks->test();
    }

    function doSomethingElse()
    {
        // you'd have to check that the method above ran and instantiated this
        // and that $this->tasks is a tasks object
        $this->tasks->blah();
    }

}

or

或者

class Foo
{
    function doStuff()
    {
        $tasks = new tasks();
        $tasks->test();
    }
}

with your code:

使用您的代码:

class Admin
{
    function validate()
    {
        // added this so it will execute
        $_SESSION['level'] = 7;

        if (! $_SESSION['level'] == 7) {
            // barMsg('YOU\'RE NOT ADMIN', 0);
            return FALSE;
        } else {
            $tasks = new Tasks();
            $tasks->test();
            $this->showPanel();
        }
    }

    function showPanel()
    {
        // added this for test
    }
}
class Tasks
{
    function test()
    {
        echo 'test';
    }
}
$admin = new Admin();
$admin->validate();

回答by davidtbernal

You're problem is with this line of code:

你的问题是这行代码:

public $tasks;
$this->tasks = new tasks();
$this->tasks->test();
$this->showPanel();

The publickeyword is used in the definition of the class, not in a method of the class. In php, you don't even need to declare the member variable in the class, you can just do $this->tasks=new tasks()and it gets added for you.

public关键字在类的定义中使用,而不是在类的方法。在 php 中,你甚至不需要在类中声明成员变量,你可以这样做$this->tasks=new tasks(),它会为你添加。