Home  >  Article  >  Backend Development  >  public方法访问的有关问题

public方法访问的有关问题

WBOY
WBOYOriginal
2016-06-13 13:12:521079browse

public方法访问的问题。
  class A{
public function pp(){
return 1300;
  }
public function __construct(){
echo 1;
}
  }
  class B{
  public function __construct(){
//echo A::__construct();
//echo A::pp();
}
  }
  $cc=new C;
?>

问个问题:
pp是class A的公有方法,所以可以用类名+双冒号的形式访问。那问题就在于,为什么class A里头的__construct方法,我也是给了public,而在class B里头,去A::__construct()调用的时候会报以下错误:

Fatal error: Non-static method A::__construct() cannot be called statically, assuming $this from incompatible context in D:\wamp\www\1.php on line 12

还有个问题,比如:[b][/b]
class A{
  public $m=10;
}
这个$m既然是public,那就应该支持类内部访问,子类访问以及实例访问。
我在有些书上看到,它们说“在任何地方”都可以访问。我想问这个任何地方是否包括其他不相关的类(和A没继承关系的类),如果支持访问,怎么访问?代码如何写的?



------解决方案--------------------
静态方法或者实例化

你上面的代码实例化了 例如这样访问

PHP code

class A{
    public $mm=10;
}
class B{
    protected $classA;
    public function __construct($number){
        $this->classA=$number;
        echo $this->classA;
    }
}
$aa=new A;
$bb=new B($aa->mm); <div class="clear">
                 
              
              
        
            </div>
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