#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Array from parent class to subclass


    Hi!

    look this simple example:

    class myClass {

    var $lang = 'en';
    var $translations = array();

    public function __construct($lang,$translations = array()) {
    $this->lang = $lang;
    $this->array_translations = $translations;
    }

    public function foo() {
    echo $this->lang;
    echo $this->array_translations['2'];
    }
    }


    class myClass2 extends myClass {
    public function __construct() {
    }

    public function foo2() {
    echo $this->lang;
    echo $this->array_translations['2'];
    }

    }


    $class = new myClass($lang,$translations);

    $class2 = new myClass2();
    $class2->foo2();

    Why array_translations doesn't read in class myClass 2? Lang yes.. but array elements no? I don't understand
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    Because
    1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
    2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this is a misunderstanding of how objects work. The $translations property is an instance property, so every object has its own $translations. Setting the value for a particular object has no effect on the other instances that might exist at this point of time.

    Think of a simpler example: If create a Mammal with the name "Peter" (could be a human, a dog, a cat, whatever), does that mean that all Humans (which are a subclass of Mammals) should now be named "Peter"?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by requinix
    Because
    1. You didn't give myClass2 the list of translations. Or the language for that matter - it's showing you "en" because that's the default value.
    2. You set up your own __construct which does not set $lang or $array_translations. If you want it to do that then (a) get rid of your version and let myClass's __construct take over, or (b) make your version call parent::__construct(...) with the right arguments.
    Thanks for your answer.

    I'have my array included in all page of my site:

    $translations = array();
    $translations['1'] = 'text1';
    $translations['2'] = 'text2';
    $translations['3'] = 'text3';
    $translations['4'] = 'text4';
    $translations['5'] = 'text5';
    $translations['6'] = 'ecc';

    I would like include this array element in my Class for being used in every subclass.

    Is it possible?
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    If the array is known from the beginning, use a static property and set the array as the default value:

    PHP Code:
    <?php

    class {

        static 
    $x = array(123);

    }

    class 
    extends {

        function 
    showX() {
            
    var_dumpself::$x );
        }

    }

    $b = new B();
    $b->showX();
    This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.

    By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
    PHP Code:
    $x = array(
        
    'a'
        
    'b'
        
    'c'
    ); 
    Last edited by Jacques1; November 24th, 2012 at 01:47 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Jacques1
    If the array is known from the beginning, use a static property and set the array as the default value:

    PHP Code:
    <?php

    class {

        static 
    $x = array(123);

    }

    class 
    extends {

        function 
    showX() {
            
    var_dumpself::$x );
        }

    }

    $b = new B();
    $b->showX();
    This way all subclasses will have a static property $x (or $translations in your case) set to a certain array. Note that the subclasses can still overwrite it. If you also want to prevent that, you have to access the property through getter/setter methods.

    By the way, why do you use number strings as array indices? And why do you set the elements one after another instead of using a much shorter array literal?
    PHP Code:
    $x = array(
        
    'a'
        
    'b'
        
    'c'
    ); 
    Ok, but my array is external... is included in all pages, and contain more elements.
    The problem is setting external array into static element.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Then use a setter method.

    PHP Code:
    <?php

    class {

        private static 
    $x;
        
        static function 
    getX() {
            return 
    self::$x;
        }
        
        static function 
    setX($x) {
            
    self::$x $x;
        }

    }

    class 
    extends {

        function 
    showX() {
            
    var_dumpself::getX() );
        }

    }

    $translations = array(
        
    'foo'
        
    'bar'
    );

    A::setX($translations);
    $b = new B();
    $b->showX();
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by Jacques1
    Then use a setter method.

    PHP Code:
    <?php

    class {

        private static 
    $x;
        
        static function 
    getX() {
            return 
    self::$x;
        }
        
        static function 
    setX($x) {
            
    self::$x $x;
        }

    }

    class 
    extends {

        function 
    showX() {
            
    var_dumpself::getX() );
        }

    }

    $translations = array(
        
    'foo'
        
    'bar'
    );

    A::setX($translations);
    $b = new B();
    $b->showX();

    Many thanks Jacques1!

    you have been useful
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0
    Originally Posted by gino8
    Many thanks Jacques1!

    you have been useful
    Ps it's strange that you have to assign the static element out of the classroom

IMN logo majestic logo threadwatch logo seochat tools logo