Professional Web Applications Themes

OOP, dynamic class extention - PHP Development

I want to be able to set a base class in a settings file, and then have other classes extend the base class, but PHP won't let me do what I have tried. I get the following error: Fatal error: Class cepweb: Cannot inherit from undefined class my_base_class in /home/me/my_child_class.php on line 10 ex: <?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_CLASSFILE { // yada yada } ?> Any thoughts on ways to overcome this? I have tried using a variable instead of a constant, ...

  1. #1

    Default OOP, dynamic class extention

    I want to be able to set a base class in a settings file, and then have other
    classes extend the base class, but PHP won't let me do what I have tried.

    I get the following error:
    Fatal error: Class cepweb: Cannot inherit from undefined class my_base_class
    in /home/me/my_child_class.php on line 10

    ex:
    <?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_CLASSFILE {
    // yada yada
    }
    ?>

    Any thoughts on ways to overcome this?

    I have tried using a variable instead of a constant, but it didn't work.

    -Jackson
    Jackson Guest

  2. #2

    Default Re: [PHP] OOP, dynamic class extention

    Shouldn't this line:
    class my_child_class extends MY_BASE_CLASSFILE {

    Read like this:
    class my_child_class extends MY_BASE_CLASS {

    After all "base_class.php" isn't the name of your class, "base_class" is.

    Jackson Miller <com> wrote:
    I want to be able to set a base class in a settings file, and then have other
    classes extend the base class, but PHP won't let me do what I have tried.

    I get the following error:
    Fatal error: Class cepweb: Cannot inherit from undefined class my_base_class
    in /home/me/my_child_class.php on line 10

    ex:

    // 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_CLASSFILE {
    // yada yada
    }
    ?>

    Any thoughts on ways to overcome this?

    I have tried using a variable instead of a constant, but it didn't work.

    -Jackson

    --
    PHP General Mailing List (http://www.php.net/)
    To unsubscribe, visit: http://www.php.net/unsub.php


    ---------------------------------
    Do you Yahoo!?
    Exclusive Video Premiere - Britney Spears
    Jough Guest

  3. #3

    Default Re: [PHP] OOP, dynamic class extention

    php is not c, you can't use constants this way.

    Jackson Miller wrote: 
    Marek Guest

  4. #4

    Default Re: [PHP] OOP, dynamic class extention

    On Thursday 30 October 2003 12:29 pm, Marek Kilimajer wrote: 
    but why? Is there a work around?

    -Jackson
     [/ref]
    Jackson Guest

  5. #5

    Default Re: [PHP] OOP, dynamic class extention

    Why? I guess because php is interpreted language and is pd only
    once, no preprocessor. Workaround is quite easy, use variable:

    <?php

    // settings to be set on install of the app
    $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
    }
    ?>
    Jackson Miller wrote: 
    >
    > but why? Is there a work around?
    >
    > -Jackson
    >[/ref]
    Marek Guest

  6. #6

    Default Re: [PHP] OOP, dynamic class extention

    On Thursday 30 October 2003 01:27 pm, Marek Kilimajer wrote: 

    I thought that too, and tried it before posting to the list (it was at the
    bottom of my first email).

    Unfortunately when you do that you get and expecting T_STRING error. The same
    thing happens if you put the var in quotes.

    Why would it not p the constant the first time? It does everywhere else.

    -Jackson

     
    > >
    > > but why? Is there a work around?
    > >
    > > -Jackson[/ref][/ref]
    Jackson Guest

  7. #7

    Default Re: [PHP] OOP, dynamic class extention

    Jackson Miller wrote: 
    >
    >
    > I thought that too, and tried it before posting to the list (it was at the
    > bottom of my first email).[/ref]
    Can you show it once again. In your first email you use a constant, not
    variable.
    Marek Guest

  8. #8

    Default Re: [PHP] OOP, dynamic class extention

    On Thursday 30 October 2003 02:15 pm, Marek Kilimajer wrote: 
    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 "p 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;
    }
    }
    ?>
    Jackson Guest

  9. #9

    Default Re: [PHP] OOP, dynamic class extention


    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:
     
    >
    > 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 "p 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;
    > }
    > }
    > ?>
    >[/ref]
    Boyan Guest

Similar Threads

  1. Dynamic Web Reference in Class Lib/GAC
    By Mike Logan in forum ASP.NET Web Services
    Replies: 5
    Last Post: January 16th, 11:03 AM
  2. Dynamic Class Instantiation with getDefinitionByName
    By Tikis Mikis in forum Macromedia Flex General Discussion
    Replies: 4
    Last Post: May 15th, 09:14 PM
  3. Dynamic Class Loading Not Working
    By moset in forum Coldfusion Server Administration
    Replies: 3
    Last Post: April 14th, 06:32 AM
  4. About dynamic class
    By isml in forum Macromedia Flex General Discussion
    Replies: 4
    Last Post: March 24th, 01:35 AM
  5. Replies: 2
    Last Post: March 7th, 06:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139