SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Apr 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Scope issue when dynamically instantiating objects with variable variables as names

    I have worked with oop, but this is my first project implementing it myself. Class getData returns an array populated with items from a specific .txt doc, based on what arguments are fed to it. Another class, WantsAndsNeeds, pulls in values a an argument, and for each one, instantiates an instance of the getData object to pull in data for use on the page. Here is my code and problem:

    public function __construct($types) {
    $models = array();

    //dynamically instantiate data objects and retrieve data
    if($types) {
    $loadThese = explode(",",$types);
    foreach($loadThese as $loadThis) {
    $name = (string)$loadThis;
    $$name = array();
    $tempObjName = $loadThis.'_obj';
    $$tempOhbjName = new loadData($loadThis);
    $$name = $$tempObjName->returnData();
    }
    }
    print_r($needs);
    }

    will return an array from a text document with the same name as the argument fed to it. This block, however, returns an error:

    public function __construct($types) {
    $models = array();

    //dynamically instantiate data objects and retrieve data
    if($types) {
    $loadThese = explode(",",$types);
    foreach($loadThese as $loadThis) {
    $name = (string)$loadThis;
    $$name = array();
    $tempObjName = $loadThis.'_obj';
    $$tempOhbjName = new loadData($loadThis);
    $$name = $$tempObjName->returnData();
    }
    }
    }
    print_r($needs);

    I think its a scope issue but have googled and tried a lot of different things, with no luck yet. Here is the code for class loadData:

    class loadData {

    //vars
    protected $data = array();


    // constructor
    public function __construct($type) {
    $raw_data = file_get_contents(dirname(dirname(__FILE__)).'/data/'.$type.'.txt');
    $raw_entries = explode("|", $raw_data);
    $raw_data = file_get_contents(dirname(dirname(__FILE__)).'/data/'.$type.'.txt');
    $raw_entries = explode("|", $raw_data);

    //check if return should be array or multidimensional array, then build
    if (strpos($raw_entries[0],',') !== false) {

    //if multidimensional array
    $innerArray = array();

    foreach($raw_entries as $item) {
    $raw_multi = explode(",",$item);
    $count = count($raw_multi);

    for ($x=1; $x<$count; $x++) {
    $innerArray[] = $raw_multi[$x];
    }

    $this->data[] = array($raw_multi[0],$innerArray);
    $pos++;
    $innerArray = array();
    }

    //if not multidimensional array
    } else {
    foreach($raw_entries as $item) {
    $this->data[] = $item;
    }
    }
    }

    // send data
    function returnData() {
    return $this->data;
    }
    }

    Can anybody see any solutions, or a better way to do this?

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rob Guthridge View Post
    ...Class getData...
    This was the first thing that jumped out at me. Classes should represent nouns. Methods should be verbs that act on that noun.

    Quote Originally Posted by Rob Guthridge View Post
    ...$$name = array();...
    I think this kind of code is frowned upon. It makes it hard to understand and predict how this function will behave, because we can't know what variables will be set without also knowing what data is being loaded.

    Quote Originally Posted by Rob Guthridge View Post
    This block, however, returns an error:

    PHP Code:
    public function __construct($types) {
            
    $models = array();
            
            
    //dynamically instantiate data objects and retrieve data
            
    if($types) {    
                
    $loadThese explode(",",$types);
                foreach(
    $loadThese as $loadThis) {
                    
    $name = (string)$loadThis;
                    $
    $name = array();
                    
    $tempObjName $loadThis.'_obj';
                    $
    $tempOhbjName = new loadData($loadThis);
                    $
    $name = $$tempObjName->returnData();
                }
            }
            }
            
    print_r($needs); 
    The print_r line needs to be inside the function.
    "First make it work. Then make it better."


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •