Re: cool OOP-phenomena!

From: Date: Thu, 11 Jan 2001 14:19:48 +0000
Subject: Re: cool OOP-phenomena!
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-33991@lists.php.net to get a copy of this message
Moritz Petersen wrote: > > Hi list, > > this is not a problem, but a nice feature of OOP, I found today. Maybe it is > useful for someone: > > <?php > // class test > > class Basis > { > var $name = "Basis"; > > function Basis($typ = false) > { > switch ($typ) > { > case "A": > $this = new Basis_A(); > break; > > case "B": > $this = new Basis_B(); > break; > } > } > The point is, that the class morphs itself into another class in the > constructor. > > Hope it helps someone, My idea would be to do the following: check if file $path_to_includes/$prefix$typ$postfix if file exists include_once that file and create an object of that type $this = new $typ(); Thus you get away from the switch case which you have to change for every new class you add to the inheritance tree. I've used this technique a few times, and I also think I've seen this used in some database wrapper classes. -- Paul K Egell-Johnsen Utvikler/PR Manager eZ systems as http://ez.no/

« previous php.general (#33991) next »