Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用来自其他对象的方法/如何传递正确的上下文?_Php - Fatal编程技术网

Php 如何使用来自其他对象的方法/如何传递正确的上下文?

Php 如何使用来自其他对象的方法/如何传递正确的上下文?,php,Php,错误: 注意:未定义的属性:Person\u Writer::$name in 如何使用来自其他对象的方法/如何传递正确的上下文? 我想在$obj中使用函数writeName() 您没有扩展基类,而且您有一个输入错误。您也没有调用writeAge()方法。下面是我的作品: class Person_Writer { function writeName ( ){ echo $this ->name; } function writeAge ( ){

错误: 注意:未定义的属性:Person\u Writer::$name in

如何使用来自其他对象的方法/如何传递正确的上下文?

我想在$obj中使用函数writeName()

您没有扩展基类,而且您有一个输入错误。您也没有调用writeAge()方法。下面是我的作品:

class Person_Writer {
    function writeName (  ){
       echo $this ->name;
    }
    function writeAge ( ){
        echo $this ->age;
    }
}

class Person {
    function __construct($name,$age) {
        $this->writer = new Person_Writer;
        $this->name= $name;
        $this->age = $age;
    }
    function __call($name, $arguments) {          
        $writter = $this->writer;
        call_user_func(array($this->writer, 'WriteName'));
        //  call_user_func(array(new Person_Writer, 'WriteName'));
    }
} 

$obj = new Person('sasha',28);        
$obj->writeName();
class个人\作者{
公共函数writeName(){
echo$this->name;
}
函数writeAge(){
echo$this->age;
}
}
类Person扩展Person\u Writer{
函数构造($name,$age){
$this->writer=newperson\u writer;
$this->name=$name;
$this->age=$age;
}
函数调用($name,$arguments){
$writer=$this->writer;
调用用户函数(数组($this->writer,'WriteName');
//调用_user_func(数组(newperson_Writer,'WriteName'));
}
} 
$obj=新人('sasha',28岁);
$obj->writeName();
回声“
”; $obj->writeAge();
我不太确定您在那里尝试做什么,如果您想调用另一个对象的函数,这将起作用:

class Person_Writer {
public function writeName (  ){
    echo $this ->name;
}
function writeAge ( ){
    echo $this ->age;
}
}

class Person extends Person_Writer{
    function __construct($name,$age) {
        $this->writer = new Person_Writer;
        $this->name= $name;
        $this->age = $age;
    }
    function __call($name, $arguments) {

        $writer = $this->writer;

       call_user_func(array($this->writer, 'WriteName'));
      //  call_user_func(array(new Person_Writer, 'WriteName'));
    }
} 

$obj = new Person('sasha',28);

$obj->writeName();
echo '<br>';
$obj->writeAge();
您为什么在您的Persone\u Writer对象中使用$this->name?这个对象不知道Person对象的变量,这就是为什么会出现未定义的错误


编辑:另一种解决方案是扩展对象的Hexana解决方案。

尝试修复打字错误$writer=$this->writer;好的,从否决票来看,我必须记住,今后不要对拼写错误提供任何帮助。你已经注意到,在一篇评论中,这已经足够好了。当提供一个答案时,询问者除了回答“修复是问题”之外。更新了我的答案,所以希望这适用于希望没有继承的OPI,但感谢您的帮助+1
class Person_Writer {
    function writeName ($name){
        echo $name;
    }
    function writeAge ($age){
        echo $age;
    }
}

class Person{
    function __construct($name,$age) {
        $this->writer = new Person_Writer;
        $this->name= $name;
        $this->age = $age;
    }
    function __call($name, $arguments) {
        $writer = $this->writer;
        $writer->writeName($this->name);
    }
} 

$obj = new Person('sasha',28);
$obj->writeName();