设为首页 收藏本站
查看: 850|回复: 0

[经验分享] PHP面向对象中的重要知识点(一)

[复制链接]

尚未签到

发表于 2015-8-23 09:57:40 | 显示全部楼层 |阅读模式
  1. __construct:
  内置构造函数,在对象被创建时自动调用。见如下代码:



<?php
class ConstructTest {
private $arg1;
private $arg2;
public function __construct($arg1, $arg2) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
print "__construct is called...\n";
}
public function printAttributes() {
print '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2."\n";
}
}
$testObject = new ConstructTest("arg1","arg2");
$testObject->printAttributes();
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2
  2. parent:
       用于在子类中直接调用父类中的方法,功能等同于Java中的super。



<?php
class BaseClass {
protected $arg1;
protected $arg2;
function __construct($arg1, $arg2) {
$this->arg1 = $arg1;
$this->arg2 = $arg2;
print "__construct is called...\n";
}
function getAttributes() {
return '$arg1 = '.$this->arg1.' $arg2 = '.$this->arg2;
}
}
class SubClass extends BaseClass {
protected $arg3;
function __construct($baseArg1, $baseArg2, $subArg3) {
parent::__construct($baseArg1, $baseArg2);
$this->arg3 = $subArg3;
}
function getAttributes() {
return parent::getAttributes().' $arg3 = '.$this->arg3;
}
}
$testObject = new SubClass("arg1","arg2","arg3");
print $testObject->getAttributes()."\n";
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3
  3. self:
       在类内调用该类静态成员和静态方法的前缀修饰,对于非静态成员变量和函数则使用this。



<?php
class StaticExample {
static public $arg1 = "Hello, This is static field.\n";
static public function sayHello() {
print self::$arg1;
}
}
print StaticExample::$arg1;
StaticExample::sayHello();
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
Hello, This is static field.
Hello, This is static field.
  4. static:
  这里介绍的static关键字主要用于PHP 5.3以上版本新增的延迟静态绑定功能。请看一下代码和关键性注释。



<?php
abstract class Base {
public static function getInstance() {
//这里的new static()实例化的是调用该静态方法的当前类。
return new static();
}
abstract public function printSelf();
}
class SubA extends Base {
public function printSelf() {
print "This is SubA::printSelf.\n";
}
}
class SubB extends Base {
public function printSelf() {
print "This is SubB::printSelf.\n";
}
}
SubA::getInstance()->printSelf();
SubB::getInstance()->printSelf();
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
This is SubA::printSelf.
This is SubB::printSelf.
       static关键字不仅仅可以用于实例化。和self和parent一样,static还可以作为静态方法调用的标识符,甚至是从非静态上下文中调用。在该场景下,self仍然表示的是当前方法所在的类。见如下代码:



<?php
abstract class Base {
private $ownedGroup;
public function __construct() {
//这里的static和上面的例子一样,表示当前调用该方法的实际类。
//需要另外说明的是,这里的getGroup方法即便不是静态方法,也会得到相同的结果。然而倘若
//getGroup真的只是普通类方法,那么这里还是建议使用$this。
$this->ownedGroup = static::getGroup();
}
public function printGroup() {
print "My Group is ".$this->ownedGroup."\n";
}
public static function getInstance() {
return new static();
}
public static function getGroup() {
return "default";
}
}
class SubA extends Base {
}
class SubB extends Base {
public static function getGroup() {
return "SubB";
}
}
SubA::getInstance()->printGroup();
SubB::getInstance()->printGroup();
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
My Group is default
My Group is SubB
  5. __destruct:
       析构方法的作用和构造方法__construct刚好相反,它只是在对象被垃圾收集器收集之前自动调用,我们可以利用该方法做一些必要的清理工作。



<?php
class TestClass {
function __destruct() {
print "TestClass destructor is called.\n";
}
}
$testObj = new TestClass();
unset($testObj);
print "Application will exit.\n";
  运行结果如下:



Stephens-Air:Desktop$ php Test.php
TestClass destructor is called.
Application will exit.
  6. __clone:
  在PHP 5之后的版本中,对象之间的赋值为引用赋值,即赋值后的两个对象将指向同一地址空间,如果想基于对象赋值,可以使用PHP提供的clone方法。该方法将当前对象浅拷贝之后的副本返回,如果想在clone的过程中完成一些特殊的操作,如深拷贝,则需要在当前类的声明中实现__clone方法,该方法在执行clone的过程中会被隐式调用。另外需要格外注意的是,__clone方法是作用再被拷贝的对象上,即赋值后的对象上执行。



<?php
class InnerClass {
public $id = 10;
public function printSelf() {
print '$id = '.$this->id."\n";
}
}
class OuterClass {
public $innerClass;
public function __construct() {
$this->innerClass = new InnerClass();
}
public function __clone() {
$this->innerClass = clone $this->innerClass;
print "__clone is called.\n";
}
}
$outerA = new OuterClass();
print "Before calling to clone.\n";
$outerB = clone $outerA;
print "After calling to clone.\n";
$outerA->innerClass->id = 20;
print "In outerA: ";
$outerA->innerClass->printSelf();
print "In outerB: ";
$outerB->innerClass->printSelf();
       运行结果如下:



Stephens-Air:Desktop$ php Test.php
Before calling to clone.
__clone is called.
After calling to clone.
In outerA: $id = 20
In outerB: $id = 10
  7. const:
      PHP5可以在类中定义常量属性。和全局常量一样,一旦定义就不能改变。常量属性不需要像普通属性那样以$开头,按照惯例,只能用大写字母来命名常量。另外和静态属性一样,只能通过类而不能通过类的实例访问常量属性,引用常量时同样也不需要以$符号作为前导符。另外常量只能被赋值为基础类型,如整型,而不能指向任何对象类型。



<?php
class TestClass {
const AVAILABLE = 0;
}
print "TestClass::AVAILABLE = ".TestClass::AVAILABLE."\n";
      运行结果如下:



0Stephens-Air:Desktop$ php Test.php
TestClass::AVAILABLE = 0
  注:该Blog中记录的知识点,是在我学习PHP的过程中,遇到的一些PHP和其他面向对象语言相比比较特殊的地方,或者是对我本人而言确实需要簿记下来以备后查的知识点。虽然谈不上什么深度,但还是希望能与大家分享。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-102869-1-1.html 上篇帖子: PHP导入Excel数据 下篇帖子: php empty()和isset()的区别
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表