oop - PHP: Is there no way to use a global variable inside an object constructor? -
i have php script intended user-editable, options set right @ beginning, including allowable extensions image files.
later on, want use in object constructor set whether file image or not.
$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 error $this->image = $this->is_image($name); } }
this doesn't work because can't call $extensions
inside class(?). there way without including $extensions
in every object of class? seems nuts declare objects
$files[] = new directory_entry($value, $extensions);
and have every instance of class have copy of same array?
why not create classe this?
class extensionsenum { const images = ["jpg", "jpeg", "gif", "png"]; }
then
use extensionsenum; class directory_entry { ... $this->is_image = in_array(strtoupper($extension), extensionsenum::images); // throws error ... }
Comments
Post a Comment