Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 oop从同一类的方法内部调用方法_Php_Oop - Fatal编程技术网

php oop从同一类的方法内部调用方法

php oop从同一类的方法内部调用方法,php,oop,Php,Oop,我有以下问题 class class_name { function b() { // do something } function c() { function a() { // call function b(); } } } 当我像往常一样调用函数时:$this->b();我得到了这个错误:当不在C中的对象上下文中时使用$this: 函数b()被声明为公共函数 有什么想法吗 我会感谢你的帮助 谢谢函数a()在方法c()中声明 有没有办法从那里称

我有以下问题

class class_name {

function b() {
   // do something
}

function c() {

   function a() {
       // call function b();
   }

}

}
当我像往常一样调用函数时:$this->b();我得到了这个错误:当不在C中的对象上下文中时使用$this:

函数b()被声明为公共函数

有什么想法吗

我会感谢你的帮助

谢谢函数
a()
在方法
c()
中声明


有没有办法从那里称呼它?我想这是最好的做法。。。好的,我就照你说的做sugested@user681982-如果你愿意,我可以给你展示一个解决你问题的例子,但这绝对不是正确的方法。让我知道。我会用正确的方式来做,但是口袋里有新把戏真是太棒了。如果不是太忙的话,你能给我看看这个吗?现在我明白为什么不推荐了,在我的情况下,它不仅是没有回报的,而且是愚蠢的。谢谢分享。击掌!
<?php

class class_name {
  function b() {
    echo 'test';
  }

  function c() {

  }

  function a() {
    $this->b();
  }
}

$c = new class_name;
$c->a(); // Outputs "test" from the "echo 'test';" call above.
<?php

class class_name {
  function b() {
    echo 'test';
  }

  function c() {
    // This function belongs inside method "c". It accepts a single parameter which is meant to be an instance of "class_name".
    function a($that) {
      $that->b();
    }

    // Call the "a" function and pass an instance of "$this" by reference.
    a(&$this);
  }
}

$c = new class_name;
$c->c(); // Outputs "test" from the "echo 'test';" call above.