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

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

WBOY
Release: 2016-06-13 12:34:55
Original
981 people have browsed it

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();
}

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template