Jump to content

Dynamically create class properties from all of constructor's arguments


Hubble

Recommended Posts

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.

Link to comment
Share on other sites

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 by Hubble
Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

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;
}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.