php学习之旅:static变量与方法

原创
2016-07-29 09:12:18 640浏览

static关键字用来修饰属性、方法,称这些属性、方法为静态属性静态方法

static的方法,只能访问static的属性,不能类访问非静态的属性。不过调用非静态方法不可以使用this关键字调用非静态方法,而必须使用self::关键字,并且被调用的非静态方法中不能有非静态变量,一般情况静态方法尽量不要调用非静态方法

static的属性,在内存中只有一份,为所有的实例共用。

可以使用self:: 关键字访问当前类的静态成员。

静态方法调用静态变量

classtest{publicstatic$pi=3.14;
        functiondisplay()
        {returnself::$pi;
        }   
    }
    $test=new test();
    echo'
'
.$test->display(); ?>

静态方法调用静态变量

classtest{publicstatic$pi=3.14;
        staticfunctiondisplay_static()
        {returnself::$pi;
        }   
    }
    $test=new test();
    echo'
'
.$test::display_static(); ?>

静态方法调用静态方法

classtest{publicstatic$pi=3.14;
        staticfunctiondisplay_static()
        {returnself::$pi;
        }   
        functiondisplay()
        {returnself::display_static();
        }
    }
    $test=new test();
    echo'
'
.$test->display(); ?>

静态方法调用非静态方法(实际就相当于将非静态方法在调用过程中转变为静态方法来处理了)

classtest{publicstatic$pi=3.14;
        staticfunctiondisplay_static()
        {returnself::display();
        }   
        functiondisplay()
        {returnself::$pi;
        }
    }
    $test=new test();
    echo'
'
.$test::display_static(); ?>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php学习之旅:static变量与方法,包括了静态方法,静态属性,静态变量方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。