For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > March 2007 > Re: [PHP-DB] Inheritance question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Inheritance question
Niel Archer

2007-03-19, 7:00 pm

Hi

> For example (see below), lets say I would like to use "public $one"
> property in a new class / method (such as class b)? What is the
> best way to accomplish this? Any help and or suggestions are welcome.
>
> class a {
> public $one;
>
> public function someMethod() {
> $this->one = '1';
> }
> }
>
> class b {
> public $two
>
> function someOtherMethod() {
> $this->two = '2';
> }
> }


At least two ways you could go about it. Most straight forward would
be to inherit, like this:

class a {
public $one;

public function someMethod() {
$this->one = '1';
}
}

class b extends a{
public $two

function someOtherMethod() {
$this->two = '2';
}
}

$object = new b;

$object->someMethod();
$object->someOtherMethod();

print $object->one, $object->two;


In addition to that, if you actually need to use an instance of a
separately to b, you can pass the instance of a into b as a parameter.
Definition for 'b' would need to be something like:

class b {
public $a;
public $two

function __Constructor($dummy) {
$this.a = $dummy;
)

function someOtherMethod() {
$this->two = '2';
}
}

$object1 = new a;
$object2 = new b(&$object1);

$object1->someMethod();
$object2->someOtherMethod();

print $object2->a->one, $object2->two;

There are more than likely other ways to go about this. Check the
documentation for further examples of how to use classes in PHP.

Niel
Dreni

2007-03-27, 12:02 am

Catherine Zeta Jone Throatjob!
http://Catherine-Zeta-Jone-Throatjo...hp?movie=148803
Tenbasder38

2007-03-27, 8:02 am

Lindsay Lohan Doing A Hung Guy!
http://Lindsay-Lohan-Doing-A-Hung-G...hp?movie=148803
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com