php class

liunx

Guest
I am trying to write an OOP for my project and i am get stuck on this. Can anyone give me some advise?

<?php
require_once ('class.UserAddress.php');

class User{
public $id;
public $name;
public $username;
public $password;


private $arrayAddress; //An array of UserAddress
//DO I NEED TO DO ANYTHING HERE?

ON THE USERADDRESS class ALREADY DEFINE EVERYTHING LIKE, do i need to do it here too?
public function __construct
public function get and set ID;
public function get and set street1;
public function get and set city;
public function get and set state;
public function get and set zip;

}

?>you first need to make the user class inherit the useraddress class.

it will need its own constructor.

However, you can use all the get and set functions from the parent class. You will just need to create your own set and get functions for properties that are only for that class.

One thing thats useful to know is, if you define a function again it replaces it. for example if your UserAddress class has a toString() function if you call it after initalising the user class it will use it. However, if you make a toString function in the user class it will use that one instead.

Hope that helps.yes the useraddress class like this
useraddress class. There is no toString in this class

class userAddress {
public $id;
public $street1;
public $city;
public $state;
public $zip;

public function __construct($id,....)
{
$this->id = $id;
$this->street = $street;
...
..}
public function getID() {..}
public function setID()
...

___________________________________
User class

require_once ('class.UserAddress.php');

class User{
public $id;
public $name;
public $username;
public $password;


private $arrayAddress; //An array of UserAddress
//DO I NEED TO DO ANYTHING HERE?

}

?>for example if your UserAddress class has a toString()
I was merley using a toString function as an example as i said.

at the moment you don't have the User class extending the userAddress class. I cant remember how to do this in PHP 5, but i guess it might be the same as PHP 4.

Change the class decleration too...
class User extends userAddress{

Now every function and property (variable) in userAddress will be available to the Userclass. However, if you add more properties, you will need the set and get methods for these. You will also need a new constructor in the new class.ok, i will try it again. thanks you for help.Note that if your child class has a constructor, then the constructor of the the class it extends will not automatically be executed. You would then need to do something like:

class User extends UserAddress{
public function __construct($var1, $var2)
{
parent::__construct($var1, $var2);
// other stuff specific to this class.....
}
}Ah, i see, I thought you *needed* a constructor in each.

In Java, i believe you can get the inherited class to fun the constructor function and then 'add to it' is this possible in PHP? I'm guessing not. lolAh, i see, I thought you *needed* a constructor in each.

In Java, i believe you can get the inherited class to fun the constructor function and then 'add to it' is this possible in PHP? I'm guessing not. lol
Yes, that's what the parent::__construct(); is doing in my example, running the constructor in the parent class. You could then continue with whatever additional functionality is needed in the child's constructor (where I have the comment line).Don't extend UserAddress into User. Inheritance is generally a IS A relation and a User IS NOT A UserAddress. Besides, what if you wanted several user addresses or maybe a UserPhoneNumber or any of several other attributes.

Your original design in which an instance of UserAddress is contained in User is correct. You just need a way for user to return the userAddress object and the application can use the userAddress methods on it.

To repeat: Avoid extending UserAddress into User.
 
Back
Top