SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic class name

    Hi, how do execute dynamic class in PHP? For example with these values coming from the database:

    PHP Code:
    include( ROOT $value['folder'] . '/' $value['class'] . '.class.php' );
    $Plugin = new $value['class'];
    $Plugin->$value['method']; 
    Both $value['class'] and $value['method'] are string stored in the database, I got the error Fatal error: Class 'PersonalClass' not found.

    Any help is greatly appreciated.
    I Dunno LOL \(_o)/

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    And what if you remove the include stuff and just stub a class at the top of the file temporarily?

    PHP Code:
    <?php

    class PersonalClass {}

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    And what if you remove the include stuff and just stub a class at the top of the file temporarily?

    PHP Code:
    <?php

    class PersonalClass {}
    Hi Cups, yes it works fine if i hardcode it. :/
    I Dunno LOL \(_o)/

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Well it would help if you didnt have a syntax error

    Hint: new classname()
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Well it would help if you didnt have a syntax error

    Hint: new classname()
    Hi SL, its not a syntax error, you can do without the parenthesis.
    I Dunno LOL \(_o)/

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    ROOT $value['folder'] . '/' $value['class'] . '.class.php' 
    The class path is likely not resolving and correctly declaring the class???

    Regards,
    Alex
    The only constant in software is change itself

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2007
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PCSpectra View Post
    PHP Code:
    ROOT $value['folder'] . '/' $value['class'] . '.class.php' 
    The class path is likely not resolving and correctly declaring the class???

    Regards,
    Alex
    Hi Alex, the file is being included right because like Cups mentioned, using hardcoding works.
    I Dunno LOL \(_o)/

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What version of PHP are you using? I cannot currently find it but previous versions of PHP had trouble with dynamic initializations of objects. If all else fails there is reflection. http://us.php.net/manual/en/book.reflection.php
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So what if you temp hardcode the actual path to the class as well:
    PHP Code:
    // include( ROOT . $value['folder'] . '/' . $value['class'] . '.class.php' );
    include( '/path/to/PersonalClass.class.php' ); 

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    What version of PHP are you using? I cannot currently find it but previous versions of PHP had trouble with dynamic initializations of objects. If all else fails there is reflection. http://us.php.net/manual/en/book.reflection.php
    5.2 is the version that began allowing dynamic class/method referencing, IIRC... It's certainly been in since then.

    Edit: I was thinking of 5.3, and that was for Static methods. Not sure when variable class instantiation was added. Still, if you're not running 5.3+, might be wise to upgrade.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  11. #11
    SitePoint Enthusiast abdussamad's Avatar
    Join Date
    May 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by justspree View Post
    Hi Alex, the file is being included right because like Cups mentioned, using hardcoding works.
    Actually I think cups asked you to hardcode the class name precisely because he suspected the file was not being included. So I would check the path to the class file.

  12. #12
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    @abdussamad - yeahm thanks, I wish I had put it that clearly.

  13. #13
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly as others are suggesting, only troubleshooting for you is; either just try once including the class file temporarily hardcoded or to echo/print the values of each element of the array $value['folder'], $value['class'], $value['method'] including ROOT:
    PHP Code:
    echo ROOT $value['folder'] . '/' $value['class'] . '.class.php'
    Does this really shows the correct value? If yes there should (must) not be the problem if you are in the PHP version of having Dynamic use of Class Names. Otherwise there is nothing to explore in your code and don't have to pull out your hair. As per my knowledge, you are on the right track. Better you let us know the PHP version that you are in and at least try to test what the guys have suggested here.

    Furthermore, I would recommend you to use require() instead of include() since require() produce FATAL error and halts the script while include will continue with an E_WARNING and maybe you have set error_reporting something to hide warnings. So try once this as well:
    PHP Code:
    error_reporting(E_ALL E_STRICT);
    ini_set('display_errors'1);
    require 
    ROOT $value['folder'] . '/' $value['class'] . '.class.php'
    Secondly I would recommend you to use Autoloading Classes in PHP which you can find here http://php.net/manual/en/language.oop5.autoload.php with some examples. If you go for it, you don't have to worry about if it is included or not it will automatically include when you create an instance of a class:
    PHP Code:
    $Plugin = new $value['class']; 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •