PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Calling a method in the parent of a parent
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Calling a method in the parent of a parent

Réponse
 
LinkBack Outils de la discussion
Vieux 09/11/2007, 09h37   #1
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Calling a method in the parent of a parent

I am currently rewriting a corporate CMS that was originally hacked
together in a hurry by a mamber of staff who is now leaving and which
is totally uncommented and is therefore in a state where maintaining
it would be a total nightmare. I'm redesigning it to take an OO
approach, but am limited to PHP 4 on the server on which it will run
and am therefore limited to that version's OO model.

The current plan is to have a hierarchy of classes going from the
general to the specific and inherit and override/extend functions as
necessary to achieve the desired results. At the top of the chain is
a CMS item object that defines props and methods that are common to
all items that will be stored by the CMS: Creation/last modified
dates, summaries, filesystem paths etc. From that I dreive classes
for specific types of CMS data. Documents, images and directories all
decend directly from the CMS item class.

As I was designing I originally intended for there to be a site class
that inherited from the item class, but as the design progressed I
realised that a site is basically a special case of a directory and
therefore should be derived from the directory class.

But here I hit a snag, because I need to access a method in the item
class while bypassing the same method in the directory class. In the
item class is a method that inserts some data into the items table of
the database. In the directory class this method is extended by
another method of the same name. This method runs the method in the
parent class with parent::method(), then does the extra SQL queries
that it needs to do for creating a directory.

The site class, however, uses a different pair of tables (item and
site) to the directory class (item and directory). This means that
the method in the site class needs to call the parent's parent method
directly without calling the parent's method. I tried both
parent::parent::method() and parent::parent -> method() but both of
these threw an error

Is there a way to call a method that's further than 1 class up the
inheritance chain?

  Réponse avec citation
Vieux 09/11/2007, 10h09   #2
John Andersen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent

On Nov 9, 11:37 am, Gordon <gordon.mc...@ntlworld.com> wrote:
> I am currently rewriting a corporate CMS that was originally hacked
> together in a hurry by a mamber of staff who is now leaving and which
> is totally uncommented and is therefore in a state where maintaining
> it would be a total nightmare. I'm redesigning it to take an OO
> approach, but am limited to PHP 4 on the server on which it will run
> and am therefore limited to that version's OO model.
>
> The current plan is to have a hierarchy of classes going from the
> general to the specific and inherit and override/extend functions as
> necessary to achieve the desired results. At the top of the chain is
> a CMS item object that defines props and methods that are common to
> all items that will be stored by the CMS: Creation/last modified
> dates, summaries, filesystem paths etc. From that I dreive classes
> for specific types of CMS data. Documents, images and directories all
> decend directly from the CMS item class.
>
> As I was designing I originally intended for there to be a site class
> that inherited from the item class, but as the design progressed I
> realised that a site is basically a special case of a directory and
> therefore should be derived from the directory class.
>
> But here I hit a snag, because I need to access a method in the item
> class while bypassing the same method in the directory class. In the
> item class is a method that inserts some data into the items table of
> the database. In the directory class this method is extended by
> another method of the same name. This method runs the method in the
> parent class with parent::method(), then does the extra SQL queries
> that it needs to do for creating a directory.
>
> The site class, however, uses a different pair of tables (item and
> site) to the directory class (item and directory). This means that
> the method in the site class needs to call the parent's parent method
> directly without calling the parent's method. I tried both
> parent::parent::method() and parent::parent -> method() but both of
> these threw an error
>
> Is there a way to call a method that's further than 1 class up the
> inheritance chain?


Just use $this->method() as the method is inherited down through the
hierarchy!
Enjoy,
John

  Réponse avec citation
Vieux 09/11/2007, 10h32   #3
Jonas Werres
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent

Gordon wrote:
> but am limited to PHP 4 on the server on which it will run


Change that. It's totally insane to design a new software for a platform
which will be supported for less than a year from now on.
  Réponse avec citation
Vieux 09/11/2007, 11h20   #4
Pavel Lepin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent


Follow-ups set to comp.object

Gordon <gordon.mcvey@ntlworld.com> wrote in
<1194601048.553776.222520@d55g2000hsg.googlegroups .com>:
> The current plan is to have a hierarchy of classes going
> from the general to the specific and inherit and
> override/extend functions as necessary to achieve the
> desired results. At the top of the chain is a CMS item
> object that defines props and methods that are common to
> all items that will be stored by the CMS: Creation/last
> modified dates, summaries, filesystem paths etc. From
> that I dreive classes for specific types of CMS data.
> Documents, images and directories all decend directly from
> the CMS item class.
>
> As I was designing I originally intended for there to be a
> site class that inherited from the item class, but as the
> design progressed I realised that a site is basically a
> special case of a directory and therefore should be
> derived from the directory class.
>
> But here I hit a snag, because I need to access a method
> in the item class while bypassing the same method in the
> directory class.


Then your design is, well, bad. What parent class extends is
its implementation detail, therefore you shouldn't rely on
it.

> In the item class is a method that inserts some data into
> the items table of the database. In the directory class
> this method is extended by another method of the same
> name. This method runs the method in the parent class
> with parent::method(), then does the extra SQL queries
> that it needs to do for creating a directory.
>
> The site class, however, uses a different pair of tables
> (item and site) to the directory class (item and
> directory).


So either make them use the same table (if Site truly is-a
Directory), or extend Item instead of Directory.

> This means that the method in the site class needs to call
> the parent's parent method directly without calling the
> parent's method. I tried both parent::parent::method()
> and parent::parent -> method() but both of these threw an
> error


I'm actually unaware if it's possible to do in PHP, because
I wouldn't do that anyway for reasons outlined above.

If you're thinking along the lines of "but Site really is a
lot like Directory" - you might as well be right, but there
are cleaner solutions to the problem. Strategy pattern
comes to mind.

--
....also, I submit that we all must honourably commit seppuku
right now rather than serve the Dark Side by producing the
HTML 5 spec.
  Réponse avec citation
Vieux 09/11/2007, 12h00   #5
Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent

On Nov 9, 10:32 am, Jonas Werres <jo...@example.org> wrote:
> Gordon wrote:
> > but am limited to PHP 4 on the server on which it will run

>
> Change that. It's totally insane to design a new software for a platform
> which will be supported for less than a year from now on.


I would if I could, but I'm just teh code monkey, and the system
that's being replaced has to remain operational on it. Said system
is, like I said, not coded to a very maintainable standard, and
updating the PHP version it's running under would almost certainly
cause it to break in some way.

  Réponse avec citation
Vieux 09/11/2007, 12h14   #6
Jonas Werres
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent

> I would if I could, but I'm just teh code monkey, and the system
> that's being replaced has to remain operational on it. Said system
> is, like I said, not coded to a very maintainable standard, and
> updating the PHP version it's running under would almost certainly
> cause it to break in some way.


I think it's worth a try on a test system. I could migrate to PHP5 with at
least that painful scripts with compatibility_mode On, which you can enable
on directory basis.
  Réponse avec citation
Vieux 09/11/2007, 12h44   #7
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a method in the parent of a parent

Gordon wrote:
> On Nov 9, 10:32 am, Jonas Werres <jo...@example.org> wrote:
>> Gordon wrote:
>>> but am limited to PHP 4 on the server on which it will run

>> Change that. It's totally insane to design a new software for a platform
>> which will be supported for less than a year from now on.

>
> I would if I could, but I'm just teh code monkey, and the system
> that's being replaced has to remain operational on it. Said system
> is, like I said, not coded to a very maintainable standard, and
> updating the PHP version it's running under would almost certainly
> cause it to break in some way.
>
>


I agree with Jonas on this. At least try the old system on PHP 5. If
it was not using any of the OO features, chances are it will not require
many (if any) changes.

You need to move sooner or later. Why lock yourself into PHP with your
new code?

I also agree with Pavel - you need to look at your design again. The
fact you're running into this problem shows it's in trouble, and that
will get worse.

But I don't necessarily agree with the fup to only comp.object.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h01.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14625 seconds with 15 queries