<?php
/*
class Test
{
//public static $a =100;
public static $b =20;
function test()
{
}
}
$t = new Test();
echo $t->test();
echo Test::$b;
*/
//静态方法和属性,静态属性不能通过一个类已实例化的对象来访问,但是静态方法可以,静态方法不需要通过对象既可以调用,$this在静态方法中不可用,静态属性不能通过->操作符来访问,静态方式调用一个非静态的方法会导致一个E_STRICT级别的错误,
class Foo
{
public static $my_static='foo';
public function staticValue()
{
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic()
{
return parent::$my_static;
}
}
print Foo::$my_static;
$foo = new Foo();
print $foo->staticValue();
//print $foo->my_static;//错误的
print $foo::$my_static;
$classname = 'Foo';
print $classname::$my_static;
$bar = new Bar();
print $bar->foostatic();
?>
php中静态方法属性的注意事项
最新推荐文章于 2024-05-28 14:55:00 发布