主页
PHP
PHP 面向对象构造函数
tryphp_constructor
运行 ❯
×
更改方向
保存代码
更改主题
<!DOCTYPE html> <html> <body> <?php class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function get_details() { echo "Name: " . $this->name . ". Color: " . $this->color .".<br>"; } } $apple = new Fruit('Apple', 'Red'); $apple->get_details(); $banana = new Fruit('Banana', 'Yellow'); $banana->get_details(); ?> </body> </html>
Apple