php 静态方法问题

WBOY
Release: 2016-06-06 20:46:09
Original
1279 people have browsed it

php 静态方法问题

<code>    private function getlong() {
    $result = unpack('Vlong', fread(self::$fp, 4));
    return $result['long'];
}
</code>
Copy after login
Copy after login

getlong 非静态方法
为什么在getClientLocation
里可以用self::getlong() 访问

回复内容:

php 静态方法问题

<code>    private function getlong() {
    $result = unpack('Vlong', fread(self::$fp, 4));
    return $result['long'];
}
</code>
Copy after login
Copy after login

getlong 非静态方法
为什么在getClientLocation
里可以用self::getlong() 访问

__callStatic

其实php的设计是可以用::方式调用实例方法,这个涉及到scope引用问题,例如:

<code class="lang-php">class A
{
    public $a;
    public function echo()
    {
        //标识为A.a
        $this->a = 'A.a';
        echo $this->a;
    }
}
Class B
{
    public $a;
    public function test()
    {
        //标识为B.a
        $this->a = 'B.a';
        //静态调用A的echo方法
        A::echo();
    }
}
//这里要不能用::调用,不然会出现context错误,因为这里要找到$this.
$b = new B();
$b->test();
//结果是:B.a
</code>
Copy after login

结果不是A.a的原因是scope,因为静态调用使得A的echo方法里的$this即scope指向了B,所以$this是指B实例,
为什么php会有这样的设计?因为 parent::foo();记得了吧,子类调用父类方法的时候,就是用parent:: 的方式调用.

Related labels:
php
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