You are here: Re: passing instance of parent class to child constructor « PHP Language « IT news, forums, messages
Re: passing instance of parent class to child constructor

Posted by Geraint Edwards on 10/28/64 11:29

Maybe I didn't explain the problem adequately. I have a specific
problem that I want to solve where I want to add to the functionality of
an existing "inbuilt" class.

For example I may be looking at a motoring class, instances of this
class are used in an existing application which I don't want to rewrite.
I want to layer in a new bit of code that intervenes everytime various
functions of the motoring class are called. For instance when the
journey function is called I want to record the mileage before calling
the parent function.

Modifying my example:

class motor_car
{
function motor_car($arg1, $arg2, $arg3)
{
// does something with $arg1, $arg2, $arg3 but doesn't keep a copy
of the parameter
}

function go_on_a_journey($fromA, $toB)
{
// does things
}
function fill_the_fuel_tank($quantityOfFuel)
{
// does things
}
}

I then have a class

class eco_monitered_car extends motor_car
{
var $mileageTravelled = 0;
var $fuelUsed = 0;
function eco_monitered_car($instanceOfMotor_Car)
{
$props = get_object_vars($instanceOfMotor_Card);
foreach ($props as $key => $val)
{
$this->$key = $val;
}
}

function go_on_a_journey($fromA, $toB)
{
$this->mileageTravelled += $toB-$fromA;
parent::go_on_a_journey($fromA, $toB);
}

function fill_the_fuel_tank($quantityOfFuel)
{
$this->fuelUsed+=$quantityOfFuel;
parent::fill_the_fuel_tank($quantityOfFuel);
}

function analyseFuelUse()
{
// This function does not exist in the parent
$efficieny = $this->mileageTravelled/$this->fuelUsed;
}
}

The application I am using provides a "slot" where I can add my code,
but this is after the instance of the "motor_car" has been created and I
no longer have access to the contructor arguments. After my slot the
"motor_car" methods go_on_a_journey and fill_the_fuel_tank are used on
many occasions. I then get the opportunity to call my analyseFuelUse
method at a later part of the application. I want to replace the
instance of a motor_car with an eco_monitored_car which would be a
transparent change from the existing appliction's point of view but
would allow me to layer in my fuel monitoring functionality.

My eco_monitored_car constructor works fine in 4.4.0 and 5.0.5 but I am
concerned that I cannot count on var_export exporting private fields
from the parent class in the future and also I should probably not be
counting on being able to write to the parent classes fields (as opposed
to using setters).

Does this now make some more sense?

Thanks

Geraint

Oli Filth wrote:
> Geraint Edwards wrote:
>
>>I have an existing instance of a class that I want to inherit from and I
>>want to create a constructor for the child that takes the parent as an
>>argument. This is an open source project that where I want to avoid
>>hacking/patching the core code and my 'component' in essence wants to
>>change the type of an existing variable.
>
>
>
> I think you misunderstand inheritance. You don't inherit from an
> *instance* of a class, you inherit from the *class* itself.
>
> It is meaningless to say that the child takes the parent as an argument
> in the constructor; not only does the parent not exist when the child
> constructor is called, but the child and the parent are the *same*
> object; a reference to one is a reference to the other (ignoring
> types).
>
> What do you mean when you say "want to change the type of an existing
> variable"? If you explain more what you're trying to achieve, we may
> able to help you with that.
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация