在php中,可以使用class
关键字来定义类。具体的语法如下所示:
class MyClass {
// class properties
public $prop1 = "I"m a class property!";
// class methods
public function __construct() {
echo "The class "", __CLASS__, "" was initiated!<br />";
}
public function __destruct() {
echo "The class "", __CLASS__, "" was destroyed.<br />";
}
public function setProperty($newval) {
$this->prop1 = $newval;
}
public function getProperty() {
return $this->prop1 . "<br />";
}
}
// create an object
$obj = new MyClass;
// get the value of $prop1
echo $obj->getProperty();
// set a new value for $prop1
$obj->setProperty("I"m a new property value!");
// get the updated value of $prop1
echo $obj->getProperty();
// destroy the object
unset($obj);