Re: abstract functions
| From: | Jens Rehsack | Date: | Sat, 09 Nov 2002 19:20:29 +0000 |
| Subject: | Re: abstract functions | ||
| References: | 1 2 3 4 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-90708@lists.php.net to get a copy of this message | ||
Leon Atkinson wrote:
How about testing for the parent? Such as: <? class AbstractClassException { } class MyClass { var $salutation;This didn't solve the problem. Each abstract class must check if it's an instantiation of sth. is itself ... Supporting an "abstract" feature like every OO-language would make it much easier. Of couse, since it is not available I use constructs like shown. I didn't need help to find such constructs, I want suggest adding the functionality for abstract methods. I accept that in ZE2 no private methods are available. I even accept that there's neither operator overloading nor multiple inheritation. And even if I miss that most: that there will no access sections like in Object Pascal or C++ (private/protected/public[/published]) and friend declaration. I just want suggest adding abstract feature, because that could make many things more OO-like and much easier to maintain... But thanks for the suggestions ... Greetings, Jensfunction MyClass() {if(!get_parent_class($this)) {throw new AbstractClassException(); }$this->salutation = 'Hello'; }function test() { print($this->salutation); } } class MyOtherClass extends MyClass { function MyOtherClass() { MyClass::MyClass(); } } //use the abstract class correctly $c = new MyOtherClass; $c->test(); //use it incorrectly, get an exception $c = new MyClass; ?> Regards, Leon
----- Original Message ----- From: "Jens Rehsack" <rehsack@liwing.de> To: "Andrei Zmievski" <andrei@gravitonic.com> Cc: <php-dev@lists.php.net> Sent: Saturday, November 09, 2002 8:22 AM Subject: Re: [PHP-DEV] abstract functions-- L i W W W i Jens Rehsack L W W W L i W W W W i nnn gggg LiWing IT-Services L i W W W W i n n g g LLLL i W W i n n g g Friesenstraße 2 gggg 06112 Halle g g g Tel.: +49 - 3 45 - 5 17 05 91 ggg e-Mail: <rehsack@liwing.de> Fax: +49 - 3 45 - 5 17 05 92 http://www.liwing.de/Andrei Zmievski wrote:ZE1 way: class MyClass { function MyClass() {That's not ok, cause (very simple) class Calcer { function Calcer() { die('abstract'); } function ShowResult( $a ) {die('MyClass is an abstract class');} } ZE2 way: class AbstractClassException { } class MyClass { function MyClass() {throw new AbstractClassException();} } On Sat, 09 Nov 2002, Jens Rehsack wrote:echo $this->CalcResult( $a )} abstract function CalcResult( $a ); } class Square extends Calcer { function Square() {Calcer::Calcer(); // cause inherited could do sth.} function CalcResult( $a ) {return $a * $a;} } class Adder extends Calcer { var $Summand; function Adder( $aSummand ) {Calcer::Calcer(); $this->Summand = $aSummand;} function CalcResult( $a ) {return $a + $this->Summand;} } I can give a more complex example, if you want, but it's not ok to die in constructor of an abstract class, cause the main logic could be implemented in this class and it needs to be derived cause for helper functions... An example is a cache control class which is able to access cached objects through it real name and is either able to store in filesystem or in database. Jens--Hi, does PHP4 with the ZE2 supports abstract function like Delphi or C++? That would be very useful for class development, cause we can avoid testing if a class is abstract if an abstract class couldn't be instantiated? Syntax could be like in C++ class X { X(){} int y() = 0; } or little bit more like pascal class X { X(){} // PHP function x(); abstract; } or class X { X(){} // PHP abstract function x(); } Greetings, Jens --L i W W W i Jens Rehsack L W W W L i W W W W i nnn gggg LiWing IT-Services L i W W W W i n n g g LLLL i W W i n n g g Friesenstraße 2 gggg 06112 Halle g g g Tel.: +49 - 3 45 - 5 17 05 91 ggg e-Mail: <rehsack@liwing.de> Fax: +49 - 3 45 - 5 17 05 92 http://www.liwing.de/-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php-Andrei http://www.gravitonic.com/* A feature is a bug with seniority. *L i W W W i Jens Rehsack L W W W L i W W W W i nnn gggg LiWing IT-Services L i W W W W i n n g g LLLL i W W i n n g g Friesenstraße 2 gggg 06112 Halle g g g Tel.: +49 - 3 45 - 5 17 05 91 ggg e-Mail: <rehsack@liwing.de> Fax: +49 - 3 45 - 5 17 05 92 http://www.liwing.de/-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php