Home >Backend Development >PHP Tutorial >成员属性在两个方法之间无法共用

成员属性在两个方法之间无法共用

WBOY
WBOYOriginal
2016-06-23 14:17:521198browse

PHP 类

class a {    public $a=0;     public fucntion ax(){        $this->a='111';     }    public fucntion bx(){        echo $this->a;     }}

$a在ax()中赋值之后,如何才能在bx()中调用值等于111的那个$a?在一个方法中给一个属性赋值之后另一个方法无法获得那个值

回复讨论(解决方案)

你的function写错了。。。。

实例化后按顺序执行 ax,bx 就有了
你要明白一点,ax/bx在类内是平行的,不是说你写在前面,后面的就有值

class a {    public $a=0;     public fucntion a(){        $this->a='111';     }     public fucntion bx(){        echo $this->a;     }}

复制我的代码,运行就可以了。 function 拼写错误,自行修改。

class a {    public $a=0;     public function a(){        return $this->a='111';     }     public function bx(){        echo $this->a();     }}$p = new a();$p->bx();

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