Go Back   WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk
Register Blogs FAQ Social Groups CalendarHelp Desk

Reply
 
LinkBack Thread Tools
Old 06-22-2009, 08:02 AM   #1
Warrior Member
 
Join Date: Jun 2009
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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?

amber.long83 is offline   Reply With Quote
Old 06-23-2009, 10:33 AM   #2
HyperActive Warrior
War Room Member
 
Join Date: Oct 2002
Posts: 360
Thanks: 112
Thanked 48 Times in 39 Posts
Default Re: Variable overloading issue in PHP

Quote:
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
CMartin is offline   Reply With Quote
Old 06-25-2009, 05:12 AM   #3
Warrior Member
 
Join Date: Jun 2009
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default 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.

amber.long83 is offline   Reply With Quote
Reply

  WarriorForum - Internet Marketing Forums > Warrior Support Forums > Programming Talk

Tags
issue, overloading, php, variable

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -6. The time now is 08:34 AM.