Heim  >  Artikel  >  Backend-Entwicklung  >  php类里的方法如何访问同类其他方法里的方法

php类里的方法如何访问同类其他方法里的方法

WBOY
WBOYOriginal
2016-06-13 10:38:14937Durchsuche

php类里的方法怎么访问同类其他方法里的方法?
class Action {

function a(){
  function b(){

}
}

function c(){
//这怎么调用a方法里的b方法???
}

}

------解决方案--------------------
千万别这样写,有什么想不开的问题,一定要这样折腾呢? 说出来,大家一起帮你想个办法。 别想不开追这种牛角尖~
------解决方案--------------------
没有什么大不了的

PHP code
class Action {  function a(){    function b(){      echo 123;    }  }  function c(){    //这怎么调用a方法里的b方法???    b();  }}$p = new Action;$p->a();$p->c();//123b();//123<br><font color="#e78608">------解决方案--------------------</font><br>哎 你这种写法有时候很容易出错。<br><br>你想c函数用b函数,那么在用之前你必须初始化a() .<br>因为a()内部对php而言是灰色的 php不知道a()里面有个b()<br>所以你不初始化a() 就用b() 肯定会出错。<br>正确的调用是先a() 在c()。<br><font color="#e78608">------解决方案--------------------</font><br>PHP一大特点,也算是悲剧就是函数定义没有作用域,放哪里都行,只要出现过。<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
class Action {    function a(){        $this->b();        }        //把b()函数抽出来吧    function b(){    }    function c(){    //这怎么调用a方法里的b方法???        $this->b();    }}<div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn