php 获取子类常量,PHP中的抽象常量 - 强制子类定义常量
发布日期:2022-02-08 20:24:00 浏览次数:32 分类:技术文章

本文共 862 字,大约阅读时间需要 2 分钟。

不,你可以尝试其他方法,如抽象方法:

abstract class Fruit

{

abstract function getName();

abstract function getColor();

public function printInfo()

{

echo "The {$this->getName()} is {$this->getColor()}";

}

}

class Apple extends Fruit

{

function getName() { return 'apple'; }

function getColor() { return 'red'; }

//other apple methods

}

class Banana extends Fruit

{

function getName() { return 'banana'; }

function getColor() { return 'yellow'; }

//other banana methods

}或静态成员:

abstract class Fruit

{

protected static $name;

protected static $color;

public function printInfo()

{

echo "The {static::$name} is {static::$color}";

}

}

class Apple extends Fruit

{

protected static $name = 'apple';

protected static $color = 'red';

//other apple methods

}

class Banana extends Fruit

{

protected static $name = 'banana';

protected static $color = 'yellow';

//other banana methods

}Source

转载地址:https://blog.csdn.net/weixin_33132553/article/details/116151396 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php 签名 验签 pkcs8,PHP和Java的RSA签名和验签
下一篇:php sublime ctr b,在 sublime 中巧用正则表达式处理文本

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年03月10日 04时10分38秒