Variable overloading issue in PHP

2 replies
Hi friends

Thanks for visiting my post.

I have a issue in object inheritance in php. I have two classes which extends other. There is one common variable name used in both the class. So it's creating a conflict while accessing the parent class variable. Let me explain this by example
PHP Code:
// First Class
class {
    public 
$email    =    "a@a.com";    
}

class 
extends {
    public 
$email    =    "b@b.com";    
}

$obj    =    new b();
echo    
$obj->email;    // this will print b@b.com but i want to print a@a.com 
Can anyone guide me?
#issue #overloading #php #variable
  • Profile picture of the author CMartin
    Originally Posted by amber.long83 View Post

    I have a issue in object inheritance in php. I have two classes which extends other. There is one common variable name used in both the class. So it's creating a conflict while accessing the parent class variable. Let me explain this by example
    PHP Code:
    // First Class
    class {
        public 
    $email    =    "a@a.com";    
    }

    class 
    extends {
        public 
    $email    =    "b@b.com";    
    }

    $obj    =    new b();
    echo    
    $obj->email;    // this will print b@b.com but i want to print a@a.com 
    Can anyone guide me?
    The way it's working is the expected way... because you are changing the parent's variable value in the subclass when you declare it.

    If you really need to declare the email variable in the subclass, then you could set the email variable in the main class to private and have a function to return the email value from the main class:
    PHP Code:
    class {
        private 
    $email "a@a.com";    
        function 
    parentEmail() {return $this->email;}
    }

    class 
    extends {
        public 
    $email "b@b.com";    
    }

    $obj = new b();
    echo 
    $obj->email// this will print b@b.com
    echo $obj->parentEmail(); // this will print a@a.com 

    What I would do is to not declare the $email variable in the subclass as it's already declared in the parent class. Class b could use a constructor where you can optionally pass an $email value - if you pass the email value you set the $email variable. An alternative is to use a setter function for the email in the subclass without using a constructor - example:
    PHP Code:
    // First Class
    class {
        public 
    $email "a@a.com";    
    }

    class 
    extends {
        function 
    setEmail($email){
                
    $this->email $email;
          } 
    }

    $obj = new b();
    echo 
    $obj->email;    // this will print a@a.com
    $obj->setEmail("b@b.com");
    echo 
    $obj->email;    // this will print b@b.com 
    Carlos
    {{ DiscussionBoard.errors[906973].message }}
  • Profile picture of the author titleless
    Banned
    [DELETED]
    {{ DiscussionBoard.errors[912749].message }}
    • Profile picture of the author amber.long83
      thanks CMartin

      your tips seems perfect. i was wrong.

      hi titleless,
      parent keyword will not work here. its only used for function not for variable.
      {{ DiscussionBoard.errors[913385].message }}

Trending Topics