Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Variable overloading issue in PHP (https://www.warriorforum.com/programming/96066-variable-overloading-issue-php.html)

adsensekit 22nd June 2009 07:02 AM

Variable overloading issue in PHP
 
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?

CMartin 23rd June 2009 09:33 AM

Re: Variable overloading issue in PHP
 
Quote:

Originally Posted by amber.long83 (Post 903302)
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

adsensekit 25th June 2009 04:12 AM

Re: Variable overloading issue in PHP
 
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.


All times are GMT -6. The time now is 08:31 PM.