大佬教程收集整理的这篇文章主要介绍了从子类php和父关键字访问父变量?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
例
class Sub extends Parent { public function foo() { echo $this -> myVar; } } class Parent { var $myVar; public function __construct() { $this -> myVar = 'a'; } }
这工作,我得到myVar的值,但@R_174_10675@用关键字parent,当我这样做时,我得到一个错误,例如,
class Sub extends Parent { public function foo() { echo parent -> myVar; } } class Parent { var $myVar; public function __construct() { $this -> myVar = 'a'; } }
编辑澄清
在访问基类的方法或基类的静态变量时,您只需要使用parent ::.如果您尝试访问基类的非静态变量,您将收到错误访问未声明的静态属性“致命错误:
这是一个让你入门的例子.
<?PHP class Animal{ protected $myVar; public function __construct() { $this->myVar = 'a'; } } class Cat extends Animal { public function foo() { echo $this->myVar; } } $cat = new Cat(); $cat->foo(); ?>
以上是大佬教程为你收集整理的从子类php和父关键字访问父变量?全部内容,希望文章能够帮你解决从子类php和父关键字访问父变量?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。