Search the Community
Showing results for tags 'constructa'.
-
I know how to dynamically create class properties like this: class Sample { /* Creates instances of classes and dynamically assigns them to properties * Example: If "test.php" is in the Classes folder, an instance of its class ... * ... will be assigned to $this->test. */ public function createInstances() { foreach(glob("Classes/*.php") as $file) { require_once $file; $fileName = basename($file, ".php"); if(class_exists($fileName)) { $lclass = strtolower($fileName); $this->{$lclass} = new $fileName(); } } } } $obj = new Sample(); $obj->createInstances(); And I'm using this in my project. But there's something I want to make using the same sort of principle - I want to dynamically create properties from all of the arguments passed to the constructor. The thing is that I don't know how many arguments will be passed. I think you can use `for` loops for this kind of thing, but I'm not sure how I would go about doing that.