Home > Backend Development > PHP Tutorial > php类里面成员属性和成员方法命名的问题

php类里面成员属性和成员方法命名的问题

WBOY
Release: 2016-06-06 20:33:57
Original
1162 people have browsed it

<code>class A {
    static $a =123;

    static function a() {
        echo self::$a;
    }
}

$a = new A;
$a -> a();
</code>
Copy after login
Copy after login

访问时候会提示Fatal error: Constructor A::a() cannot be static in C:\AppServ\www\test.php on line 48

把方法名改成b()访问就行了,这是为什么?搞不明白

回复内容:

<code>class A {
    static $a =123;

    static function a() {
        echo self::$a;
    }
}

$a = new A;
$a -> a();
</code>
Copy after login
Copy after login

访问时候会提示Fatal error: Constructor A::a() cannot be static in C:\AppServ\www\test.php on line 48

把方法名改成b()访问就行了,这是为什么?搞不明白

与类名同名的成员方法等同于构造方法 __construct。而构造方法不允许为static

另外,static属性的成员方法,建议静态调用而非实例化后调用。直接A::b()即可

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