除了使用传统的class关键字声明PHP类之外,还有以下几种方式:
$object = new class {
public function sayHello() {
echo "Hello, World!";
}
};
$object->sayHello();
推荐的腾讯云相关产品:腾讯云云服务器(CVM),产品介绍链接:https://cloud.tencent.com/product/cvm
trait Greeting {
public function sayHello() {
echo "Hello, World!";
}
}
class MyClass {
use Greeting;
}
$object = new MyClass();
$object->sayHello();
推荐的腾讯云相关产品:腾讯云云函数(SCF),产品介绍链接:https://cloud.tencent.com/product/scf
interface Greeting {
public function sayHello();
}
class MyClass implements Greeting {
public function sayHello() {
echo "Hello, World!";
}
}
$object = new MyClass();
$object->sayHello();
推荐的腾讯云相关产品:腾讯云容器服务(TKE),产品介绍链接:https://cloud.tencent.com/product/tke
abstract class Greeting {
abstract public function sayHello();
}
class MyClass extends Greeting {
public function sayHello() {
echo "Hello, World!";
}
}
$object = new MyClass();
$object->sayHello();
推荐的腾讯云相关产品:腾讯云云数据库MySQL版(TencentDB for MySQL),产品介绍链接:https://cloud.tencent.com/product/cdb_mysql
领取专属 10元无门槛券
手把手带您无忧上云