/    Sign up×
Community /Pin to ProfileBookmark

Constructor Arguments in Child Class Problem

At the moment I have 2 a “Photo” class and a “Photo_Small” class which extends the photo class.

Here is the code for these (simplified for readability):-

[CODE]

class Photo{

protected $src;
protected $username;
protected $size;
protected $classname = “regular”;

public function __construct($src,$username){

$this->src = $src;
$this->username = $username;

}

public function render(){

return ‘<img title=”‘.$this->username.'” class=”‘.$this->classname.'” alt=”‘.$this->username.'” width=’.$this->size.’ height=’.$this->size.’ src=”images/’.$this->src.’.jpg”>’;

}

}

class Photo_Small{

public function __construct(){

$this->size = 50;
$this->classname = “small”;

}

}

[/CODE]

The above code works fine as long as I do this:-

[CODE]

$photo = new Photo(1,”john123″);
echo $photo->render();

[/CODE]

However I want to be able to create small photos easily (baring in mind the Photo_Small class will have a lot more things going on in it’s constructor than in my example here) like this:-

[CODE]

$photo = new Photo_Small(1,”john123″);
echo $photo->render();

[/CODE]

But in this last example the constructor of the Photo_Small class is overwriting the parent constructor so none of the arguments passed in are being applied.

How can I get it to work so that I can just type “new Photo_Small(1,”john123″)” and all of the arguments will be passed, the Photo constructor will fire and then the child constructor will fire?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 15.2012 — You often end up using [b]parent::[/b] to access methods/properties from the parent class. As a trivial example:
[code=php]
<?php
class Foo
{
private $test;

public function __construct()
{
$this->test = "Test";
}

public function getTest()
{
return $this->test;
}
}

class Bar extends Foo
{
private $something;

public function __construct($something)
{
$this->something = $something;
parent::__construct();
}

public function getTest()
{
return parent::getTest() . ' ' . $this->something;
}
}

$bar = new Bar('something');
echo $bar->getTest(); // "Test something"
?>[/code]
Copy linkTweet thisAlerts:
@jimmyoneshotauthorJun 15.2012 — Ah right but what if I want to pass arguments to the parent constructor? Like this:-

[code=php]new Photo(1,"john123");[/code]

but also need to be able to pass it to the child constructor like this:-

[code=php]$photo = new Photo_Small(1,"john123");[/code]

The only way I can think of doing this is defining those arguments in BOTH the parent constructor and in the child constructor like this:-

[code=php]public function __construct($src,$username){[/code]

This seems a bit over the top.

I also want to create a Photo_Large class which would also extend the photo class and this would mean again rewriting these same constructor arguments into each constructor and when accessing each parent constructor from within a child constructor over and over again like this:-

[code=php]

class Photo{

protected $src;
protected $username;
protected $size;
protected $classname = "regular";

public function __construct($src,$username){

$this->src = $src;
$this->username = $username;

}

public function render(){

return '<img title="'.$this->username.'" class="'.$this->classname.'" alt="'.$this->username.'" width='.$this->size.' height='.$this->size.' src="images/'.$this->src.'.jpg">';

}

}

class Photo_Small extends Photo{

public function __construct($src,$username){

parent::__construct($src,$username);
$this->size = 50;
$this->classname = "small";

}

}

class Photo_Large extends Photo{

public function __construct($src,$username){

parent::__construct($src,$username);
$this->size = 500;
$this->classname = "large";

}

}

[/code]


Or is that just the way it is generally done? Doesn't seem easily updateable. It feels as though I'm doing something wrong ?
Copy linkTweet thisAlerts:
@NogDogJun 15.2012 — Yes, it is typical to pass arguments via the parent::__construct() call. Generally, I would recommend trying to keep the parameter list the same for both parent and child constructors, so that you can use either in a given situation without having to change how you call it, but that is not required by PHP. I often have something like this in a child class:

[code=php]
public function __construct($a, $b)
{
parent::__construct($a, $b);
$this->somethingNotDoneInParentConstructor();
}
[/code]

Or in this case, perhaps your child class needs a new paramenter:
[code=php]
public function __construct($a, $b, $c)
{
parent::__construct($a, $b);
$this->c = $c;
}
[/code]

But in this case, should you ever want some sort of dynamic instantiation that decides at run-time whether to use the parent or child class, you would then also need to add logic to know whether to add that additional parameter to the instantiation. Not a huge problem, but can be a bit messy to maintain.
Copy linkTweet thisAlerts:
@jimmyoneshotauthorJun 15.2012 — Ah right gotchya. I thought that may be the case. It does seem tricky to maintain yes, which was my worry, although I suppose it does make sense as the constructor is meant to be for the current class so it really should therefore have arguments passed to it.

Thanks very much for the help. I'm learning a lot from here ?
Copy linkTweet thisAlerts:
@NogDogJun 15.2012 — No problem: this kind of stuff is more interesting than the usual "mysql_fetch_assoc() expects parameter 1 to be a resource..." question. ?
×

Success!

Help @jimmyoneshot 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.20,
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,
)...