简体   繁体   English

从构造函数调用成员函数时出现“调用未定义函数”错误

[英]“Call to undefined function” error when calling member function from constructor

Well this is depressing, but probably some little oversight I imagine. 好吧,这令人沮丧,但我想可能有点疏忽。

I have this PHP class: 我有这个PHP类:

class SearchModel
{
    private $searchOptions;

    public function __construct()
    {
        $this->searchOptions = populateSearchOptions();
    }

    public function getSearchOption()
    {
        return $this->searchOptions;
    }

    private function populateSearchOptions()
    {
        $this->searchOptions = array(
            // values....
        );
    }
}

When I instantiate this class, PHP returns: 当我实例化此类时,PHP返回:

Fatal error: Call to undefined function populateSearchOptions() in SearchModel.php ...

When changing: 更改时:

$this->searchOptions = populateSearchOptions();

to: 至:

$this->searchOptions = $this->populateSearchOptions();

the complaint is gone but the contents of $searchOptions is NULL . 投诉已消失,但$searchOptions的内容为NULL

So... if I call populateSearchOptions() this way from inside the constructor, instantiate the class and try to output the contents of the array: 所以...如果我从构造函数内部以这种方式调用populateSearchOptions() ,请实例化该类并尝试输出数组的内容:

$this->model = new SearchModel();
// Output contents of model->searchOptions

The output is NULL. 输出为NULL。 But when I do (and not bother trying to call populateSearchOptions() from the constructor): 但是,当我这样做时(不必费心尝试从构造函数中调用populateSearchOptions() ):

$this->model = new SearchModel();
$this->model->populateSearchOptions();
// Output contents of $this->model->searchOptions

It outputs the values assigned to $this->model->searchOptions as desired. 它根据需要输出分配给$this->model->searchOptions的值。

Anyone know what's going on here? 有人知道这是怎么回事吗? I'd like to simply be able to call populateSearchOptions() from the SearchModel constructor. 我只想能够从SearchModel构造函数调用populateSearchOptions()

Change the constructor from 将构造函数从

public function __construct()
{
    $this->searchOptions = populateSearchOptions();
}

to

public function __construct()
{
    $this->populateSearchOptions();
}

1) always use $this in php to call non static class methods and variables 1)始终在php中使用$this来调用非静态类方法和变量

2)The result was null before because the populateSearchOptions function does not return a value so you were effectively setting a value to it in the function then setting it to null immediately after. 2)之前的结果为null,因为populateSearchOptions函数未返回值,因此您实际上在函数中为其设置了一个值,然后立即将其设置为null

alternatively you can change the populateSearchOptions function to 或者,您可以将populateSearchOptions函数更改为

private function populateSearchOptions()
{
    return array(
        // values....
    );
}

and do effectively the same thing. 并有效地做同样的事情。

Output contents of model->searchOptions is NULL model-> searchOptions的输出内容为NULL

it is null because it is a private variable 它为null,因为它是一个私有变量

try making the searchOptions public then it will be good: 尝试将searchOptions公开,那么会很好:

class SearchModel
{
    public $searchOptions;

    public function __construct()
    {
        $this->searchOptions = $this->populateSearchOptions();
    }

    private function populateSearchOptions()
    {
        $this->searchOptions = array(
            // values....
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM