#1
  1. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374

    Access static variable from parent class?


    so I am trying to access static variable from a parent class but it doesnt seem to be working?

    I get:
    Fatal error: Access to undeclared static property:

    PHP Code:
    class Common {

    public static function 
    find_all(){
       return 
    self::find_by_sql("SELECT * FROM ".static::$table_name);    
     } 
    //this static call works

    protected function attributes(){
            
    $attributes = array();
            foreach(static::
    $db_fields as $field){ //this one creates problems
                
    if(property_exists($this$field)){
                    
    $attributes[$field] = $this->$field;
                }
            }
            return 
    $attributes;
        }

    now child class:

    PHP Code:
    class Test extends Common {
       protected static 
    $table_name="table_name"
       protected static 
    $db_fields = array('id',name');

    [/php]
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    This works for me, with a bit of hacking to get something testable out of what you gave:

    PHP Code:
    <?php

    class Common 

      function 
    attributes(){ 
            
    $attributes = array(); 
            foreach(static::
    $db_fields as $field){ //this one creates problems 
                
    if(property_exists($this$field)){ 
                    
    $attributes[$field] = $this->$field
                } 
            } 
            return 
    $attributes
        } 
    }  


    class 
    Test extends Common 
       protected static 
    $table_name="table_name";  
       protected static 
    $db_fields = array('id','name'); 
       
       public function 
    __construct() {
         
    $this->id 123;
         
    $this->name 'abc';
       }
    }  


    $test = new Test();
    $attr $test->attributes();

    print_r($attr);
    Aside from removing the protection from the attributes() function (necessary for calling it from the global scope) it's the same.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374
    cheers for the quick reply. but that doesn't work for me in the way i am trying to use it, i should have been more clearer but i thought it wouldnt work anyway you tried..

    PHP Code:
    class Common {
        
        public static function 
    find_all(){
            return 
    self::find_by_sql("SELECT * FROM ".static::$table_name);    
        }
        
        public static function 
    find_by_sql($sql=""$params){
            global 
    $database;
            
            
    $result_set $database->query($sql$params);
        
    $object_array = array();
            foreach ( 
    $result_set as $row ){ 
            
    $object_array[]=self::instantiate($row);
        }    
        return 
    $object_array;
        }
        
         static function 
    instantiate($record){
        
    $object = new self();
        foreach(
    $record as $attribute=>$value){
            if(
    $object->has_attribute($attribute)){
            
    $object->$attribute $value;
            }
        }
        return 
    $object;
         }
        
         function 
    has_attribute($attribute) { //
            
    $object_vars $this->attributes();
            return 
    array_key_exists($attribute$object_vars);
        }
        
        function 
    attributes(){
            
    $attributes = array();
            foreach(static::
    $db_fields as $field){  ////////////////////////////////here is the error
                
    if(property_exists($this$field)){
                    
    $attributes[$field] = $this->$field;
                }
            }
            return 
    $attributes;
        }    

    i think the following may be the problem!
    $object = new self();
  6. #4
  7. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by paulh1983
    cheers for the quick reply. but that doesn't work for me in the way i am trying to use it
    Where's the exact problem you're having, then?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374
    using the "common" class above, i have another class which is child. it has two protected static variables (Post 1).

    i am trying to do something like:

    $test = new Questions();
    print_r( $test->find_all() );

    and i get the Fatal error: Access to undeclared static property:

    EDITED: if i move some of those functions instantiation and below from COMMON to the questions class, it all works perfectly but then i will have to copy/paste those to each class and i would rather not do it.

    it does look like the "instantiation" method that creates a self is wrong as i am thinking it is creating "Common" class self rather than child?
    Last edited by paulh1983; July 9th, 2013 at 10:49 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,969
    Rep Power
    374
    SOLVED..

    $class = get_called_class();
    $object = new $class();

    did this in the instantiate class to get the child class and instantiate that!

    cheers

IMN logo majestic logo threadwatch logo seochat tools logo