|
-
July 26th, 2009, 09:03 PM
#1
PHP and Classes - OO - Accessing other classes
I am quite new to PHP but am making good progress.
One problem I have is understanding how to access one class from another.
As an example I have created two classes
PHP Code:
class address{ private $addressline1; private $addressline2; private $suburb; etc...
function __constructor(params){ code....; }
function set_value($param){ code...; }
function get_address(){
echo $this->addressline1."\n";
}
} //End class Address
class person{
private $firstname; private $lastname; private $residential_address; private $postal_address; etc...
function __constructor(params){ code....; }
function set_value($param){ code...; }
function get_person(){ echo $this->firstname . " " . $this->lastname."\n"; etc.... }
Public function set_residential_address(address $a){ $this->residential_address = $a; }
} //End class person
When I use these two classes I am doing the following
PHP Code:
$mark = new person('Mark','1234556','03-Jul-1964'); $new_address = new address('Magnolia cottage','',8,'Margaret','Street','Northmeade'); $new_address->get_address(); $mark->set_residential_address($new_address);
My issue or problem is how do I display the individual address details.
What sort of class constructs do I need in the person class.
Or to display the details once I've assigned values and assigned the Person->Residential_address do I just use the
$new_address->get_address
I would think a well developed person or user class and address class would be usefel in
lots of applications.
thanks
Mark
Last edited by PeejAvery; July 27th, 2009 at 10:58 AM.
Reason: Added PHP tags.
-
July 27th, 2009, 05:45 AM
#2
Re: PHP and Classes - OO - Accessing other classes
Create an instance of address in the person class
PHP Code:
class address{
private $addressline1; private $addressline2; private $suburb; public $operson;
function makeperson() { $operson = new person(); $operson->firstname = "John"; $operson->lastname = "Doe"; }
}
You need to change the visibility of person variables in the person class, does not work if they are private
Last edited by PeejAvery; July 27th, 2009 at 10:59 AM.
Reason: Changed code to PHP tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|