class hw
{
public static function hi()
{
return 'Hello World';
}
}
echo hw::hi();//Output:Hellp World
class hw2
{
public function hi()
{
return 'Hello Wrold';
}
}
echo hw2::hi();//Output:Hellp World从上例看出,使用了static和不适用static属性以后都可以直接使用::方法从外面直接调用。但是为了效率和其他方面因素,还是建议使用static来限制。
static类内部调用方法
class foo
{
    private static function c()
    {
        return 'abcde';
    }
    public static function a()
    {
        echo self::c();
    }
    public static function b()
    {
        echo $this->c();
    }
    public function  e()
    {
        echo self::c();
    }
}
foo::a();//Output:abcde
foo::b();//Output:Fatal error: Using $this when not in object context in
foo::e();//Output:abcefstatic属性
class foo
{
public static $a;
public static function a()
{
self::$a = 'abcd';
}
}
foo::a();//Output:abcde
echo foo::$a;static继承与使用
class foo
{
public static $a;
public static function a()
{
return 'abcde';
}
}
class soo extends foo
{
public static function a()
{
echo '12345';
}
}
soo::a();//Output:12345class foo
{
public static $a;
public static function a()
{
return 'abcde';
}
}
class soo extends foo
{
public static function a()
{
echo parent::a();
}
}
soo::a();//Output:12345class foo
{
public static $a;
public static function a()
{
return 'abcd';
}
}
class soo extends foo
{
public static function aa()
{
echo self::a();
}
}
soo::a(); 以上就介绍了PHP静态方法和属性,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号