如何在类构造函数中定义常量? [英] How to define constant in class constructor?

查看:374
本文介绍了如何在类构造函数中定义常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在类构造函数中定义一个类常量

Can I define a class constant inside the class constructor function ?

(基于某些条件)

推荐答案

这违背了类常量的思想 - 它们不应该依赖于特定的实例。你应该使用一个变量。

That goes against the idea of class constants - they should not be dependent on a specific instance. You should use a variable instead.

然而,如果你坚持这样做,是非常冒险,可以安装PHP扩展,你可以看看runkit扩展允许在运行时修改类及其常量。请参阅此文档: http://www.php.net/manual/en/function。 runkit-constant-add.php

However, if you insist on doing this, are very adventurous and can install PHP extensions, you can have a look at the runkit extension that allows to modify classes and their constants at runtime. See this doc: http://www.php.net/manual/en/function.runkit-constant-add.php

这篇关于如何在类构造函数中定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆