/    Sign up×
Community /Pin to ProfileBookmark

Another OOP beginner question

Hi,

I’m having some trouble with php inheritance and constuctors and I would like to know the right way this is done so I can debug the code more easily.

If the parent class has a set of properties (a,b,c) that are given values in the constructor and a child class adds a few more properties (d,e,f) that are given values in its constructor, how do i call the child class? I can think of many ways to do this but none seems correct(or works). What values do i pass to the child class?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@CoutJan 18.2009 — You can always just from parents to children inherit methods and variables.

Example:
[code=php]<?php

class parentClass
{
public $a = null;

public function __construct()
{
$this -> a = "Add_A";
}
}

class childClass extends parentClass
{
public $b = null;

public function __construct()
{
parentClass::__construct();
$this -> b = "Add_B";
}
}

$parent = new parentClass();
// exists in class parentClass
echo "<br/>parent a : " . $parent -> a;
// does not exists in class parentClass
echo "<br/>parent b : " . $parent -> b;
// delete object
unset( $parent );

$child = new childClass();
// exists in class parentClass and childClass
echo "<br/>child a : " . $child -> a;
echo "<br/>child b : " . $child -> b;
// delete object
unset( $child );
?>[/code]
Copy linkTweet thisAlerts:
@RoxyBooauthorJan 18.2009 — What I was talking about is this:

<?php

class parentClass
{
public $a = null;

public function __construct($a)
{
$this -> a = $a;
}
}

class childClass extends parentClass
{
public $b = null;

public function __construct($a,$b)
{
parentClass::__construct($a);
$this -> b =$b;
}
}

$parent = new parentClass('Add_a');
// exists in class parentClass
echo "<br/>parent a : " . $parent -> a;
// does not exists in class parentClass
echo "<br/>parent b : " . $parent -> b;
// delete object
unset( $parent );

$child = new childClass('Add_a', 'Add_b');
// exists in class parentClass and childClass
echo "<br/>child a : " . $child -> a;
echo "<br/>child b : " . $child -> b;
// delete object
unset( $child );

?>

Now it works OK in php4 (I didn't know of :: ). I have to repeat 'Add_a' twice though (once to the parebt and once to the kid).

You can pass variables to the constructor in php5, can't you?

I was so sure I had picked a good webhost...
Copy linkTweet thisAlerts:
@CoutJan 18.2009 — Year, in php5 I can pass vars to the constructor. And please use the BB Code "php" for the code. Do you pay for your webhost?
Copy linkTweet thisAlerts:
@RoxyBooauthorJan 18.2009 — I pay alright,

And it's even more on the expensive side.
Copy linkTweet thisAlerts:
@NogDogJan 18.2009 — Note that there is no [b]__construct()[/b] "magic method" in PHP4. You instead have to use a method with the same name as the class as the constructor. (PHP5 supports this, too, if you use it [i]instead[/i] of the __construct() method.)

But you really want to move to PHP5 if at all possible in order to take advantage of the much-enhanced OOP support it provides over PHP4.
×

Success!

Help @RoxyBoo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...