Unexpected Result from Calling a Non-static Function with class::method()
来源:互联网 发布:怎么进入淘宝店铺 编辑:程序博客网 时间:2024/06/03 13:20
The static keyword, which does not exist in PHP 4, is imported since PHP 5. And there's also a new usage (PHP 5.3) static::method(), which enables a method derived from a parent class to call methods or access constants in an object of a derived class.
It's known that non-static methods can be called with the syntax class::method(), just like a static function. But weird result may come. The problem I came across this morning was as follows.
<?phpclass A{public function GetTableName(){return static::NAME;}}class B extends A{const NAME = 'B';}class C{const NAME = 'C';public function getBTableName(){return B::GetTableName();}}$c = new C();echo $c->getBTableName();?>
I expected the output should be B. Unfortunately, it was C. I've tried several possible solutions and it worked when I made A::GetTableName() static.
The solution:
<?phpclass A{static public function GetTableName(){return static::NAME;}}class B extends A{const NAME = 'B';}class C{const NAME = 'C';public function getBTableName(){return B::GetTableName();}}$c = new C();echo $c->getBTableName();?>
I thought a non-static method works just as it's static if called with class::method(), the way a static one is called. And PHP allows that. But they can't work exactly the same.
static::NAME is actually C::NAME, though class C is not derived from class A, when non-static B::GetTableName() is called statically inside class C.
static::NAME will still be B::NAME when static B::GetTableName() is called.
Non-static methods doesn't always act like static ones any more, if called with class::method().
That's what I draw. Possibly wrong~ Syntax of PHP is what I have been learning for years but don't really understand.
- Unexpected Result from Calling a Non-static Function with class::method()
- Non-static method cannot be referenced from a static context
- Calling a v8 javascript function from c++ with an argument
- Cannot make a static reference to the non-static method setInAnimation(Animation) from the type View
- Cannot make a static reference to the non-static method getLocalActivityManager() from the type Acti
- Calling a Java Class from PowerBuilder 10
- C++ Can't Overload Static Function with Non-Static Function
- The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one
- 出现警告:incompatible pointer types returning from a function with result type
- Incompatible pointer types returning 'NSArray *' from a function with result type 'xxx *'
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- Cannot make a static reference to the non-static method
- Calling Java Class Methods from C with JNI
- non-static method
- Dynamically Calling a Function
- matlabr: a Package to Calling MATLAB from R with system
- matlabr: a Package to Calling MATLAB from R with system
- Mac 显示隐藏文件
- Linux学习笔记: 操作文件及目录的13个命令
- 语音识别:现状
- iOS strong 和weak的形象理解
- JUnit4的测试类
- Unexpected Result from Calling a Non-static Function with class::method()
- 微软面试题<二>
- ubuntu配置jdk7.0过程
- iphone中 CoreData 框架的简单解释
- 第七周任务三
- ajax实例2
- NetBeans 中开发 Yii
- 【通信】TCP建立连接协议是三次握手,而关闭连接却是四次握手
- 常用语句(二)