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
相关推荐:
php面向对象 静态延迟绑定static::
浅谈php面向对象编程
以上就是关于php面向对象中一些方法、函数应用的代码的详细内容。
