Jump to content

Extending A Class


The Little Guy

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. :D

Link to comment
Share on other sites

... 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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.