public function __construct() { parent::__construct(); // 调用父类构造方法 echo '这里是子类', PHP_EOL; }
public function __construct() { parent::__construct(); echo "子类之" . PHP_EOL; }
class Person { public $name; public $age; function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person1 = new Person("John", 30); echo "Name: " . $person1->name . "
"; echo "Age: " . $person1->age;
Name: John Age: 30
namespace MyPackage\Person; class Person { public $name; public $age; function __construct($name, $age) { $this->name = $name; $this->age = $age; } }This example shows the usage of the Person constructor function in a namespace called "MyPackage\Person". The constructor function initializes the class properties in the same way as in the previous example. The namespace declaration at the beginning of the code indicates that this class belongs to a package/library called "MyPackage".
public function __construct() { parent::__construct(); // 调用父类构造方法 echo '这里是子类', PHP_EOL; }
public function __construct() { parent::__construct(); echo "子类之" . PHP_EOL; }