http://codepad.viper-7.com/I0Zqoi
我不明白这有什么问题,或者如何解决它或为什么.对于编程有一点了解的人可以解释一下幕后发生的事情,比如在翻译层面吗?另外,我如何解决我的问题,为什么我需要以纠正的方式编写我的代码?你能用人类的语言告诉我,这有什么问题以及如何让它变得更好?我想我的书不能很好地解释,其中的代码不起作用.:/ 谢谢.
class A
{
private $foo = 'bar';
function read()
{
echo self::$foo;
}
}
$a = new A();
a::read();
Strict Standards: Non-static method A::read() should not be called statically in /code/I0Zqoi on line 13
Fatal error: Access to undeclared static property: A::$foo in /code/I0Zqoi on line 8
Run Code Online (Sandbox Code Playgroud)
唯一的解决方法似乎是在方法前添加"静态".显然,非静态方法不能被静态装置访问(例如,类A {函数读(){回声"无所谓"};}不能由一个访问::读(),因为 - >运算是必要的).此外,静态属性不能由目标代码访问,即使他们在函数中存在(例如,类A {静态$变量;功能读(){回声这 - $>变量};} A->读();韩元不行,因为 - >运算符被用来访问一个调用静态属性的函数.).通过将方法和属性都更改为静态,可以通过静态方式访问该方法.通过将方法和属性更改为非静态,使得可以使用对象实例访问任一方法.对我来说调试器抛出错误是没有意义的,因为我的书说静态属性可以通过非静态方法通过对象代码调用从非静态方法调用.那么,调试器坏了吗?因为我已经尝试了所有组合,并且如果方法和属性都是静态的或非静态的,代码似乎只能起作用.:(((
Mic*_*ski 12
严格标准部分是因为a::read()在静态上下文中调用::.在声明$a为类实例之后A,您应该read()使用->运算符调用变量上的方法:
// Proper non-static method call
$a = new A();
$a->read();
Run Code Online (Sandbox Code Playgroud)
在类定义中,$foo声明为私有属性,但没有static关键字.然后在静态上下文中使用::运算符而不是->.访问它的正确方法是L.
// Proper reference to non-static $foo
function read() {
echo $this->foo;
}
Run Code Online (Sandbox Code Playgroud)
现在static无论如何意味着什么?静态方法和属性引用所有当前和未来类实例共享的类方法和属性.如果A::$foo被宣布为:
private static $foo;
Run Code Online (Sandbox Code Playgroud)
然后在你的代码中只有一个$foo类的所有类A.变化$foo会影响到该类的所有实例A,并且$foo可以甚至没有创建类的实例来访问(如new A();)
同样,可以在不创建类实例的情况下调用静态方法,因为它们不会修改非静态的类属性.
// Static method call:
A::read();
Run Code Online (Sandbox Code Playgroud)
要声明属性和方法static,只需添加static关键字:
// Property
private static $foo;
// Method
public static function foo() {}
Run Code Online (Sandbox Code Playgroud)
编辑更多示例:
class A
{
// Private property (non-static)
private $foo;
// Public property (static)
public static $bar = 12345;
// Public (non-static) function to access private $foo
public function getFoo() { return $this->foo; }
// Public (non-static) function to set private $foo
public function setFoo($newfoo) { $this->foo = $newfoo; }
// Static function
public static function incrementBar() { self::$bar++; }
}
Run Code Online (Sandbox Code Playgroud)
现在看看它在行动:
// We haven't created any class instances yet (with the 'new' operator)
// But we can access the static properties & functions:
echo A::$bar . " ";
// prints 12345
A::incrementBar();
echo A::$bar . "\n";
// prints 12346
// Now we'll start working with class instances.
// Create 2 instances of class A
$a = new A();
$a->setFoo(8888);
$b = new A();
$b->setFoo(9999);
// It's a violation of strict standards to call getFoo() statically
// And it's meaningless to do so, because $foo only exists inside a class instance!
// Can't do this... Issues a strict warning since we're calling non-static getFoo() statically
//echo A::getFoo();
// But we can call getFoo() on the class instances:
echo $a->getFoo() . " " . $b->getFoo() . "\n";
// Prints 8888 9999
// Remember $bar was 12346...
echo $a::$bar . " " . $b::$bar . "\n";
// Prints 12346 12346
// Now modify the static property $bar again
// This affects all class instances.
A::incrementBar();
echo $a::$bar . " " . $b::$bar . "\n";
// Prints 12347 12347
Run Code Online (Sandbox Code Playgroud)
我把这整个东西塞进了键盘:http://codepad.viper-7.com/tV6omK
你正在阅读的书一定不能注意严格的标准.如果非静态函数不尝试访问/修改非静态属性,则可以静态成功调用它,但它会发出严格警告.如果非静态函数确实修改或访问非静态属性$this->property,则将是致命错误.你不能这样做.
在PHP error_reporting中E_ALL,显示所有错误的设置实际上不包括严格警告.必须这样做E_ALL & E_STRICT.