我有一个用户可编辑的php脚本,一开始就设置了一些选项,包括允许的图像文件扩展名。
稍后,我想在对象构造函数中使用它来设置文件是否是图像。
df.copy(deep=True)
这不起作用,因为我无法从类(?)内部调用$extensions = ["jpg", "jpeg", "gif", "png"];
class directory_entry {
function __construct($name) {
$this->name = $name;
$extension = pathinfo($name, PATHINFO_EXTENSION);
$this->is_image = in_array(strtoupper($extension), $extensions); // throws an error
$this->image = $this->is_image($name);
}
}
。有没有办法在不在此类的每个对象中包含$extensions
的情况下执行此操作?似乎坚果用
$extensions
并让这个类的每个实例都有一个相同数组的副本?
答案 0 :(得分:0)
为什么不为此创建一个classe?
class ExtensionsEnum
{
const IMAGES = ["jpg", "jpeg", "gif", "png"];
}
然后
use ExtensionsEnum;
class directory_entry {
...
$this->is_image = in_array(strtoupper($extension), ExtensionsEnum::IMAGES); // throws an error
...
}
答案 1 :(得分:0)
似乎最适合我正在尝试做的解决方案(请记住我是PHP和OOP n00b)是globals关键字:
$extensions = ["jpg", "jpeg", "gif", "png"];
class directory_entry {
function __construct($name) {
$this->name = $name;
$extension = pathinfo($name, PATHINFO_EXTENSION);
global $extensions; //use the global version of $extensions
$this->is_image = in_array(strtoupper($extension), $extensions); // works!
$this->image = $this->is_image($name);
}
}