Search the Community
Showing results for tags 'properties'.
-
class foo { // was this always allowed? public $bar = 'abc default value'; // or did older versions only allow this? public $bar; } I couldn't determine the answer to this question looking at documentation. I found some old code where no class had a default value. Maybe it was a developer preference, but I was wondering if older PHP versions didn't even allow default values? Just a question of curiosity.
- 2 replies
-
- class
- properties
-
(and 1 more)
Tagged with:
-
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.