Home  >  Article  >  Backend Development  >  php中一个类访问另一个类中的成员函数的步骤

php中一个类访问另一个类中的成员函数的步骤

WBOY
WBOYOriginal
2016-06-13 12:34:55947browse

php中一个类访问另一个类中的成员函数的方法
类里写个静态方法,用来返回$a实例,随处调用

class instance{
     private static $a;
     static function getA(){
             if(!self::$a){
                   self::$a = new a();
           }
           return self::$a;
    }
}

class a{
       function ita(){
          echo 'ita function';
}
}

class b{
      function itb(){
     //在这里取得a的单例,不会出现多次实例化的现象
    $a=instance::getA();   
     $a->ita();
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn