Re: [PHP] OOP, dynamic class extention

From: Boyan Nedkov (k24d88_at_zonnet.nl)
Date: 10/31/03


Date: Fri, 31 Oct 2003 01:39:20 +0100
To:  php-general@lists.php.net


Hey guys, you can extend a _class_ from an existing _class_ but not from
a _string_constant_ (Ex1 - MY_BASE_CLASS is evaluated to constant of
type string) neither from a _string_variable_ (Ex2 - $my_base_class_var
is a variable of type string).

To be able to use different base classes with the same name just change
the reference to the different settings files that contain the class
declared with the same name but with different functionality, perhaps in
the following way:

<?php
   $a = 1;
   // blah blah
   if ($a==1) {
     include_once("settingsfile1.php");
   } else {
     inclide_once("settingsfile2.php");
   }
?>

Or even (much) better create a single base class in a single
configuration file and different modified instances of that class used
for each of the different configurations you may have.

Boyan

Jackson Miller wrote:

> On Thursday 30 October 2003 02:15 pm, Marek Kilimajer wrote:
>
>>Can you show it once again. In your first email you use a constant, not
>>variable.
>
> Sure. In my first email I said that I had tried with a variable too (but
> didn't show the code).
>
> Ex1 gives a "cannot inherit from undefined class" error
> Ex2 gives a "parse error, expecting T_STRING"
>
> Ex1 (with constants):
> <?php
>
> // settings to be set on install of the app
> define("MY_BASE_CLASS","base_class");
> define("MY_BASE_CLASSFILE","base_class.php");
>
> // require the class file
> require_once(MY_BASE_CLASSFILE);
>
> class my_child_class extends MY_BASE_CLASS {
> // yada yada
> }
> ?>
>
> Ex2 (with variables):
> <?php
>
> // settings to be set on install of the app
> define("MY_BASE_CLASS","base_class");
> define("MY_BASE_CLASSFILE","base_class.php");
> $my_base_class_var = MY_BASE_CLASS;
>
> // require the class file
> require_once(MY_BASE_CLASSFILE);
>
> class my_child_class extends $my_base_class_var {
> // yada yada
> }
> ?>
>
> Also, just to be clear:
>
> base_class.php
> <?php
>
> class base_class {
> function base_class() {
> echo "it works";
> return true;
> }
> }
> ?>
>