The Little Guy Posted May 15, 2009 Share Posted May 15, 2009 I have two classes: - User - Bank Bank extends User, and what I want to do is get a public property from User, which is $id, in my file, I have something like this: $user = new User(); $user->user_setup(); $bank = new Bank(); echo $bank->owner; class User{ public $id, $first, $last, $email; public function user_setup(){ $this->id = $_SESSION['id']; $this->first = $_SESSION['first']; $this->last = $_SESSION['last']; $this->email = $_SESSION['email']; } } class Bank extends User{ public $owner; public function __construct(){ $this->owner = parent::$id; } } From what I have, I was expecting "$this->owner" from the Bank class, to equal my user id from the User class, which in this case should be 1. Any reason why it is not, am I doing it wrong? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/ Share on other sites More sharing options...
.josh Posted May 15, 2009 Share Posted May 15, 2009 properties from parent classes are used the same way as if it were declared in the child class. That is: $this->id is what you want to use. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835031 Share on other sites More sharing options...
allworknoplay Posted May 15, 2009 Share Posted May 15, 2009 properties from parent classes are used the same way as if it were declared in the child class. That is: $this->id is what you want to use. Agreed, and you should be able to do this too correct? $bank->user_setup(); Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835033 Share on other sites More sharing options...
.josh Posted May 15, 2009 Share Posted May 15, 2009 Well I suppose it depends on where you're trying to call it, don't you think? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835036 Share on other sites More sharing options...
cringe Posted May 16, 2009 Share Posted May 16, 2009 Little Guy, You should consider making your class variables private or protected with public setters/getters. If class variables are public, a consumer can use them directly which breaks encapsulation and causes future problems if you decide to rework your class code. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835068 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 properties from parent classes are used the same way as if it were declared in the child class. That is: $this->id is what you want to use. Then why is that not working? I tried, but I get nothing... In my construct I have this: $this->owner = $this->$id; echo $this->owner; Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835089 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 You need to call the user_setup() method first Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835092 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 You need to call the user_setup() method first Like inside the Bank class? And not like this: $user = new User(); $user->user_setup(); $bank = new Bank(); echo $bank->owner; ? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835144 Share on other sites More sharing options...
trq Posted May 16, 2009 Share Posted May 16, 2009 You need to call the user_setup() method first Like inside the Bank class? And not like this: $user = new User(); $user->user_setup(); $bank = new Bank(); echo $bank->owner; Yes. In your example $bank and $user are two completely different objects. ? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835161 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 If I did it like this would it work, where I call user_setup from the extended Bank class, instead of the User class? $bank = new Bank(); $bank->user_setup(); echo $bank->owner; Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835364 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 Why would Bank extend User? Inheritance denotes an is-a relationship between the parent and child class. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835367 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 because "Bank" is the users bank account, it contains his/her banking information, such as: Total cash Loans Deposits/Withdrawals etc. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835402 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 Yeah, but the Bank is not the User. You should be using composition instead. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835406 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 Without the user, you wouldn't have a bank account, because the bank account requires a user.... That is how I have been looking at this situation, I'm I looking at it the wrong way? If you were to make this how would you go about it? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835420 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 The Little Guy, to put Daniel0's words into examples you can understand - I have 2 classes - Vehicle and Car. <?php class Vehicle { private $plate_number; public function __construct ($plate) { $this->plate_number = $plate; } public function getPlateNumber () { return $this->plate_number; } } class Car extends Vehicle { private $num_doors; public function __construct ($num_doors, $plate_number) { parent::__construct($plate_number); $this->num_doors = $num_doors; } } I have Car extends Vehicle because a Car is a Vehicle; however a Vehicle is not always a Car. Just like a Bank is not a User. However, you can say Bank and User is a has-a relation instead of an is-a relation, just like a Vehicle and Car is a has-a relation, but Car and Vehicle is an is-a relation. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835427 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 OK, That makes sense, but would it be possible for you to explain to me why Bank and User are not a good relationship? I'm not sure I understand that part (this is the first time I have use inheritance). Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835432 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 Is a Bank a User or is a User a Bank? No. You can say a User is a Human. It's just logic. Edit - to expand, think about what a Bank class does. Or better yet, think of the characteristics of a Bank. It has no relationship with a User. Banks need Users, but it isn't a User. It's like saying you need Aor, so let's have a Human extends Air. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835433 Share on other sites More sharing options...
The Little Guy Posted May 16, 2009 Author Share Posted May 16, 2009 OK, so basically don't extend User with Bank. So if you were to extend Bank, what types of things would be good extensions of Bank? Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835436 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 ... hmm... well... it depends. There are no set answer for that question. It just depends on the situation and what you want to code. I mean it's just pure logic. It's not really even programming-related. I guess you can say Bank extends Institution because a Bank is an Institution. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835441 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 So if you were to extend Bank, what types of things would be good extensions of Bank? Anything that logically is a particular type of bank. Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835445 Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 Yeah like BoA (Bank of America). <?php class BoA extends Bank { ... } Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835447 Share on other sites More sharing options...
Daniel0 Posted May 16, 2009 Share Posted May 16, 2009 Well, that depends on your implementation. For that to be true then you could say that an instance of BoA would be a particular branch of BoA. You couldn't say e.g. Daniel0 extends PHPFreaks_Member. It might not always be the case that there would be a logical choice for a subclass. If you want a good example of how hierarchical inheritance you could take a look at the biological taxonomy for living organism. Overall you have life. Within life you have the different domains, and within the individual domains you have kingdoms, and so on... Quote Link to comment https://forums.phpfreaks.com/topic/158322-extending-a-class/#findComment-835450 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.