Re: PHP 4.0 Bug #5343: a way of implementing class methods
| From: | waldschrott | Date: | Mon, 03 Jul 2000 15:22:54 +0000 |
| Subject: | Re: PHP 4.0 Bug #5343: a way of implementing class methods | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-23329@lists.php.net to get a copy of this message | ||
<bulk including="a very descriptive example">
I think they´re (almost) well designed, but to make code more readable, I grouped several classes, easier (and faster) would be if there would be ANY way to equip classes. Require()/include() is not possible, the last enhancement report you´ve commented (class:newmethod()...)won´t be realized too, then I don´t see there any better solution.
Say you´ve some functions in your own function library, they´re created for use in classes. The only way to equip some classes with these functions is to copy&paste the code into the class definition. It´s not possible to add methods later and it´s not possible to require()/include() anything inside the class definition.
Sure you could pack them into a helper_functions_class and create new instances from inside the classes that need equipment and then you could pass the object itself ($this) to the helper_functions_class to gain access to the originating class, but I don´t think that´s the fine way. I could also create a helper_class in the originating constructor and map it to $this->helper=new helper_functions_class(); to avoid creating the same instance multiple times.
</bulk>
Ok. maybe I´m wrong, thus have a look on this:
<example>
Ability pool: sword_fighting,boxing,cloaking,lock_picking,be_feared
Heroes: warrior,thief,amateur_thief
First I need a generic hero:
class hero {
function boxing() {} }
class warrior extends hero {
function sword_fighting() {} }
class thief extends hero {
function cloaking() {}
function lock_picking() {} }
class amateur_thief extends hero {
function lock_picking() {}
function be_feared() {} }
Thus you see I´ve written the method lock_picking() two time, so I could create a basic_thief_class, what would make code overall smaller and avoid redundancies...
class basic_thief extends hero {
function lock_picking() {} }
class thief extends basic_thief {
function cloaking() {} }
class amateur_thief extends basic_thief {
function be_feared() {}}
</example>
Now please don´t tell me that I´m totally wrong in any point... :)
regards
Jouni Ahto wrote:
On Sun, 2 Jul 2000, waldschrott wrote: It´s sometimes needed to equip classes with some standard methods and making class X a subclass of Y of Z of A of B is not the fine way ;) I've got a hunch that your class hierarchy isn't designed the right way. Although, I'm not very object oriented. Unless that object is something I can either eat or drink. :) -- Jouni Ahto-- o----------0-¬---------O-·---¬----o---®-----o o O ° . | http://www.kiffen.de | pRoteçt y0ur bRaín |0 O ° ¤ ° · 0°·³°²'²³-¹'³´³°^°³~³²³°'³²²¨³²^³¹³²°²³`³º³°Þ ° o © ° . · | psychedelic experience | gott@kiffen.de | O ° o ° o-¬--o--0-----©-·--O-----o-----0-¤----------o 0 ° · ° . ¤ ·