Hubble Posted February 22, 2014 Share Posted February 22, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/ Share on other sites More sharing options...
.josh Posted February 22, 2014 Share Posted February 22, 2014 Little bit unclear, perhaps a pseudo-code example might help clarify what you really want, but it sounds like maybe you want func_get_args Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/#findComment-1470099 Share on other sites More sharing options...
Hubble Posted February 23, 2014 Author Share Posted February 23, 2014 (edited) Little bit unclear, perhaps a pseudo-code example might help clarify what you really want, but it sounds like maybe you want func_get_args Ok, so here's an example: a.php: <?php include 'b.php'; $yes = "yes"; $no = "no"; $maybe = "maybe"; $obj = new b($yes, $no, $maybe); It passes 3 variables to b(). I want to have other things like that which will pass different amounts. b.php: class b { public function __construct() { $args = func_get_args(); // Loop through all variables somehow $this->{all variables} = {value of all variables} // Create properties of each variable with their values $this->example(); } public function example() { echo $this->yes; // This should output "yes" } } Edited February 23, 2014 by Hubble Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/#findComment-1470273 Share on other sites More sharing options...
Hubble Posted February 23, 2014 Author Share Posted February 23, 2014 A better way of explaining it: I want to create properties in a class with the names of all of the variables passed to the constructor and with the same values. Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/#findComment-1470276 Share on other sites More sharing options...
mac_gyver Posted February 23, 2014 Share Posted February 23, 2014 (edited) at call time, any variables that were forming the calling parameter list have been replaced with their value, so there's no way of knowing the name they had (parameters can be literal values, variables, other functions...) the best you could do would be to use the parameter number for referencing them or you would need to form an array with keys/values, the keys being the name you want to use, and then use that array as a parameter to the constructor. using the array method, you would simply store that array as a class property and use a magic __get method to read any value using it's name. Edited February 23, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/#findComment-1470281 Share on other sites More sharing options...
Hubble Posted February 23, 2014 Author Share Posted February 23, 2014 at call time, any variables that were forming the calling parameter list have been replaced with their value, so there's no way of knowing the name they had (parameters can be literal values, variables, other functions...) the best you could do would be to use the parameter number for referencing them or you would need to form an array with keys/values, the keys being the name you want to use, and then use that array as a parameter to the constructor. using the array method, you would simply store that array as a class property and use a magic __get method to read any value using it's name. Thanks. My finished code (copy and pasted so it looks horrible): $yes = "yup"; $no = "no"; $arr = array("yes" => $yes, "no" => $no); $obj = new test($arr); class test { public function __construct(Array $arr) { foreach($arr as $key => $value) { $this->{$key} = $value; } $this->init(); } public function init() { echo $this->yes; } } Quote Link to comment https://forums.phpfreaks.com/topic/286421-dynamically-create-class-properties-from-all-of-constructors-arguments/#findComment-1470283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.