PHP中创建对象有哪些具体方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计699个文字,预计阅读时间需要3分钟。
创建对象实例:PHP创建对象的六种方式
1. 使用new关键字phpclass Person { private $name='bruce'; public function getName() { return $this->name; }}$person=new Person();
2. 使用构造函数phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}$person=new Person('bruce');
3. 使用工厂方法phpclass Person { private $name='bruce'; public function getName() { return $this->name; }}function createPerson($name) { return new Person($name);}$person=createPerson('bruce');
4. 使用单例模式phpclass Person { private static $instance=null; private $name='bruce'; public function getName() { return $this->name; } private function __construct($name) { $this->name=$name; } public static function getInstance($name) { if (self::$instance===null) { self::$instance=new Person($name); } return self::$instance; }}$person=Person::getInstance('bruce');
5. 使用反射phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}$reflection=new ReflectionClass('Person');$person=$reflection->newInstance('bruce');
6. 使用继承phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}class Student extends Person { private $age=20; public function getAge() { return $this->age; }}$student=new Student('bruce');
创建对象实例:
<?php /** * PHP创建对象的六种方式 */ class Person{ private $name = 'bruce'; public function getName(){ return $this->name; } public static function getObjBySelf(){ return new self(); } //动态延迟绑定,能识别调用者 public static function getObjcByStatic(){ return new static(); } } //Person子类 Teacher class Teacher extends Person{ public static function getObjByParent(){ return new parent(); } } //1、new 类名();创建对象 $obj1 = new Person();//等价于写法 $obj1 = new Person; echo '类名:'.get_class($obj1).'<br>'; echo $obj1->getName().'<hr>'; //2、将类名字符串赋值给一个变量,通过变量创建 $clsName = 'Person'; $obj2 = new $clsName(); echo '类名:'.get_class($obj2).'<br>'; echo $obj2->getName().'<hr>'; //3、通过对象实例创建对象 $obj3 = new $obj2(); echo '类名:'.get_class($obj3).'<br>'; echo $obj3->getName().'<hr>'; //4、通过 new self() //$obj4 = (new $obj3())->getObjBySelf(); $obj4 = Person::getObjBySelf(); echo '类名:'.get_class($obj4).'<br>'; echo $obj4->getName().'<hr>'; //5、通过 new parent() $obj5= Teacher::getObjByParent(); echo '类名:'.get_class($obj5).'<br>'; echo $obj5->getName().'<hr>'; //6、通过 new static(); $obj6 = Person::getObjcByStatic(); echo '类名:'.get_class($obj6).'<br>';//类名:Person echo $obj6->getName().'<hr>'; //bruce //当用子类去调用时候,发现static自动识别当前调用者(静态延迟绑定),返回当前调用者对象 $obj7 = Teacher::getObjcByStatic(); echo '类名:'.get_class($obj7).'<br>';//类名:Teacher echo $obj7->getName().'<hr>';//bruce $obj8 = Person::getObjBySelf(); echo '类名:'.get_class($obj8).'<br>';//类名:Person echo $obj8->getName().'<hr>'; //new self()在子类中调用依旧返回原来父类的绑定 $obj9 = Teacher::getObjBySelf(); echo '类名:'.get_class($obj9).'<br>';//类名:Person echo $obj9->getName().'<hr>';
运行结果:
推荐教程:PHP视频教程
以上就是PHP创建对象的6种方式的详细内容,更多请关注自由互联其它相关文章!
本文共计699个文字,预计阅读时间需要3分钟。
创建对象实例:PHP创建对象的六种方式
1. 使用new关键字phpclass Person { private $name='bruce'; public function getName() { return $this->name; }}$person=new Person();
2. 使用构造函数phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}$person=new Person('bruce');
3. 使用工厂方法phpclass Person { private $name='bruce'; public function getName() { return $this->name; }}function createPerson($name) { return new Person($name);}$person=createPerson('bruce');
4. 使用单例模式phpclass Person { private static $instance=null; private $name='bruce'; public function getName() { return $this->name; } private function __construct($name) { $this->name=$name; } public static function getInstance($name) { if (self::$instance===null) { self::$instance=new Person($name); } return self::$instance; }}$person=Person::getInstance('bruce');
5. 使用反射phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}$reflection=new ReflectionClass('Person');$person=$reflection->newInstance('bruce');
6. 使用继承phpclass Person { private $name='bruce'; public function getName() { return $this->name; } public function __construct($name) { $this->name=$name; }}class Student extends Person { private $age=20; public function getAge() { return $this->age; }}$student=new Student('bruce');
创建对象实例:
<?php /** * PHP创建对象的六种方式 */ class Person{ private $name = 'bruce'; public function getName(){ return $this->name; } public static function getObjBySelf(){ return new self(); } //动态延迟绑定,能识别调用者 public static function getObjcByStatic(){ return new static(); } } //Person子类 Teacher class Teacher extends Person{ public static function getObjByParent(){ return new parent(); } } //1、new 类名();创建对象 $obj1 = new Person();//等价于写法 $obj1 = new Person; echo '类名:'.get_class($obj1).'<br>'; echo $obj1->getName().'<hr>'; //2、将类名字符串赋值给一个变量,通过变量创建 $clsName = 'Person'; $obj2 = new $clsName(); echo '类名:'.get_class($obj2).'<br>'; echo $obj2->getName().'<hr>'; //3、通过对象实例创建对象 $obj3 = new $obj2(); echo '类名:'.get_class($obj3).'<br>'; echo $obj3->getName().'<hr>'; //4、通过 new self() //$obj4 = (new $obj3())->getObjBySelf(); $obj4 = Person::getObjBySelf(); echo '类名:'.get_class($obj4).'<br>'; echo $obj4->getName().'<hr>'; //5、通过 new parent() $obj5= Teacher::getObjByParent(); echo '类名:'.get_class($obj5).'<br>'; echo $obj5->getName().'<hr>'; //6、通过 new static(); $obj6 = Person::getObjcByStatic(); echo '类名:'.get_class($obj6).'<br>';//类名:Person echo $obj6->getName().'<hr>'; //bruce //当用子类去调用时候,发现static自动识别当前调用者(静态延迟绑定),返回当前调用者对象 $obj7 = Teacher::getObjcByStatic(); echo '类名:'.get_class($obj7).'<br>';//类名:Teacher echo $obj7->getName().'<hr>';//bruce $obj8 = Person::getObjBySelf(); echo '类名:'.get_class($obj8).'<br>';//类名:Person echo $obj8->getName().'<hr>'; //new self()在子类中调用依旧返回原来父类的绑定 $obj9 = Teacher::getObjBySelf(); echo '类名:'.get_class($obj9).'<br>';//类名:Person echo $obj9->getName().'<hr>';
运行结果:
推荐教程:PHP视频教程
以上就是PHP创建对象的6种方式的详细内容,更多请关注自由互联其它相关文章!

