php教程|php手册
前端学PHP之面向对象系列第四篇——关键字
php教程-php手册
html导航条源码,ubuntu的厂家是,tomcat32位绿色,土壤 小爬虫,温泉php授权系统 课程,seo klzw
×
东西介绍网站源码,ubuntu定时删除文件,tomcat等常用中间件,c#爬虫系统,php单个视频教程外包,seo工具参数lzw
目录
[1]public [2]protected [3]private[4]final[5]static[6]const[7]this[8]self[9]parent
虾米音乐源码,ubuntu启动后卡在,怎样配置tomcat8,爬虫箱圆形,php日期代码,搜索引擎优化(SEO)推广方案lzw
前面的话
php实现面向对象的一个显著特征是大量使用关键字,本文将详细介绍关键字
public
public表示公有,它具有最大的访问权限,被定义为公有的类成员可以在任何地方被访问
如果属性用 var 定义,则被视为公有,如果方法没有设置关键字,则该方法默认为公有
test($d1->public);//1000?>
protected
protected表示受保护的,被定义为受保护的类成员则可以被其自身以及其子类和父类访问
test();//111?>
private
private表示私有的,被定义为私有的类成员则只能被其定义所在的类访问
private); }}$d1 = new demo;$d1->test();//1?>
final
PHP5新增了final关键字,它只能用来修饰类和方法,不能使用final这个关键字来修饰成员属性,因为final是常量的意思,我们在PHP里定义常量使用的是define()函数和const关键字,所以不能使用final来定义成员属性
如果父类中的方法被声明为final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承
static
static关键字表示静态的意思,用于修饰类的成员属性和成员方法(即为静态属性和静态方法)
类中的静态属性和静态方法不用实例化(new)就可以直接使用类名访问
[注意]静态属性不能通过一个类已实例化的对象来访问,但静态方法可以
由于静态方法不需要通过对象即可调用,所以伪变量 $this 在静态方法中不可用,静态属性不可以由对象通过 -> 操作符来访问
用静态方式调用一个非静态方法会导致一个 E_STRICT 级别的错误
就像其它所有的 PHP 静态变量一样,静态属性只能被初始化为文字或常量,不能使用表达式。所以可以把静态属性初始化为整数或数组,但不能初始化为另一个变量或函数返回值,也不能指向一个对象
staticValue() . "\n";//fooprint $foo::$my_static . "\n";//fooprint $foo->my_static . "\n"; //报错 ?>
const
可以把在类中始终保持不变的值定义为常量。在定义和使用常量的时候不需要使用$符号,而是使用const
常量的值必须是一个定值,不能是变量,类属性,数学运算的结果或函数调用
showConstant();//constant valueecho $class::constant."\n";//constant value?>
this
当一个方法在类定义内部被调用时,有一个可用的伪变量this,特殊对象的引用this就是在对象内部的成员方法中,代表本对象的一个引用,但只能在对象的成员方法中使用,不管是在对象内部使用$this访问自己对象内部成员。还是在对象外部通过对象的引用名称访问对象中的成员,都需要使用特殊的运算符“->”来完成访问
[注意]this在静态方法中不可用
foo();//$this is defined (A) A::foo();//$this is not defined. $b = new B();$b->bar();//$this is defined (B) B::bar();//$this is not defined.?>
self
在类的方法中,不能用this来引用静态变量或静态方法,而需要用self来引用
showConstant();//constant value?>
parent
parent用于调用父类中定义的成员方法或常量
test();//111a?>
// <![CDATA[var all = document.getElementById(cnblogs_post_body).children;var select = [];for(var i = 1; i < all.length; i++){if(all[i].getAttribute(id)){if(all[i].getAttribute(id).match(/anchor\d+$/)){select.push(all[i]);}}}var wheel = function(e){e = e || event;var data;if(e.wheelDelta){data = e.wheelDelta;}else{data = -e.detail * 40;}for(var i = 0; i 0){return;}if(select[i].getBoundingClientRect().top 0){change(oCon.children[i+2])}}else{change(oCon.children[select.length+1])}}}document.body.onmousewheel = wheel;document.body.addEventListener(‘DOMMouseScroll’,wheel,false);var oCon = document.getElementById(“content”);var close = oCon.getElementsByTagName(‘span’)[0];close.onclick = function(){if(this.innerHTML == ‘显示目录’){this.innerHTML = ‘×’;this.style.background = ”;oCon.style.border = ‘2px solid #ccc’;oCon.style.width = ”;oCon.style.height = ”;oCon.style.overflow = ”;oCon.style.lineHeight = ’30px’;}else{this.innerHTML = ‘显示目录’;this.style.background = ‘#3399ff’;oCon.style.border = ‘none’;oCon.style.width = ’60px’;oCon.style.height = ’30px’;oCon.style.overflow = ‘hidden’;oCon.style.lineHeight = ”;}}for(var i = 2; i < oCon.children.length; i++){oCon.children[i].onmouseover = function(){this.style.color = #3399ff;}oCon.children[i].onmouseout = function(){this.style.color = inherit;if(this.mark){this.style.color = #3399ff;}}oCon.children[i].onclick = function(){change(this);}}function change(_this){for(var i = 2; i < oCon.children.length; i++){oCon.children[i].mark = false;oCon.children[i].style.color = inherit;oCon.children[i].style.textDecoration = one;oCon.children[i].style.borderColor = ransparent;}_this.mark = true;_this.style.color = #3399ff;_this.style.textDecoration = underline;_this.style.borderColor = #2175bc;}// ]]>