<?php
// 定义一个简单的类
class Person {
// 属性
public $name;
public $age;
// 构造函数
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
// 方法
public function introduce() {
return "Hello, my name is " . $this->name . " and I am " . $this->age . " years old.";
}
}
// 创建对象
$person1 = new Person("Alice", 30);
// 调用对象的方法
echo $person1->introduce();
?>
class Person
定义了一个名为 Person
的类,它有两个属性 $name
和 $age
。__construct
是类的构造函数,在创建对象时自动调用,用于初始化对象的属性。introduce
是一个方法,返回一段介绍文本。$person1 = new Person("Alice", 30);
创建了一个 Person
类的对象,并传递参数给构造函数。$person1->introduce();
调用了对象的方法并输出结果。上一篇:php移除数组元素
下一篇:php 安装redis扩展
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站