这篇文章主要介绍了关于PHP面向对象中的一些知识点,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1. __construct:
内置构造函数,在对象被创建时自动调用。见如下代码:
<?phpclass 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。
<?phpclass 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。
<?phpclass 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以上版本新增的延迟静态绑定功能。请看一下代码和关键性注释。
<?phpabstract 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仍然表示的是当前方法所在的类。见如下代码:
<?phpabstract 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 B<span>!本文来源gaodai#ma#com搞*!代#%^码网5</span><pre>搞gaodaima代码
ase {} class SubB extends Base { public static function getGroup() { return "SubB"; }} SubA::getInstance()->printGroup();SubB::getInstance()->printGroup();