www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] instantiate class from variable

  1. #1
    Join Date
    Feb 2008
    Posts
    19

    resolved [RESOLVED] instantiate class from variable

    I think I'm losing the plot. Can anyone help me spot the issue as I can't see what I'm doing wrong

    I simply want to instantiate a class from variable. There isn't any kind of hidden magic involved but I'm obviously missing something.

    PHP Code:

    /*this is a generated string which is returned from a 
    function that determines a route. For the purpose of 
    this example I'll just hard code it but it definitely 
    returns this exact string*/
    $pageController "defaultController";

    $page = new $pageController;
    //I've also tried $page = new $pageController(); 
    Returns:
    Fatal error: Class 'defaultController' not found in ... on line 97
    But strangely, if I do this:
    PHP Code:

    $pageController 
    "defaultController";

    $page = new defaultController
    It works.

    Using:
    PHP 5.5.9 (cli) (built: Feb 5 2014 13:02:39)
    Copyright (c) 1997-2014 The PHP Group
    Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,330
    This works for me (PHP 5.4.12):
    PHP Code:
    <?php

    class Foo
    {
        public function 
    test()
        {
            return 
    "hello";
        }
    }

    $class 'Foo';

    $test = new $class();

    echo 
    $test->test();
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2008
    Posts
    19
    Thanks NogDog I appreciate any help you can give.

    I've ran the simple test above and that works fine.

    I'm not sure if this is perhaps a namespace issue or not so I've been messing about with them.

    When I try to instantiate the class with a namespace included I don't get an error however if I try to address any of the class methods I get the error:

    \main\application\controllers\defaultController
    Fatal error: Call to a member function test() on a non-object in C:\...\main\application\controllers\controller.php on line 108
    PHP Code:
    <?php namespace main\application\controllers;

    class 
    Controller
    {
        public function 
    load()
        {
            
    $pageController 'defaultController';

            
    $nsPageController "\main\application\controllers\\" $pageController;
            
    $page $nsPageController;
            
            
    $page->test();
        }
    //!EOF : /main/application/controllers/controller.php
    The defaultController class is located within the same directory and simply extends the controller class above.

    PHP Code:
    <?php namespace main\application\controllers;

        class 
    defaultController extends Controller
        
    {
            public function 
    index()
            {
                echo 
    "hello world";
            }

            public function 
    test()
            {
                echo 
    "I'm in test mode";
            }

        }
    //!EOF : /main/application/controllers/defaultcontroller.php
    I didn't think I needed to call defaultController with a namespace since it uses exactly the same namespace as the parent class. This was added as a precaution to see if this solved the problem. I'm new to namespacing so maybe I've misunderstood some of the concepts/uses

  4. #4
    Join Date
    May 2014
    Posts
    905
    In my experience PHP namespaces are a disaster... I've never had anything behave as expected when trying to use them; they outright break variable variables, variables as class names, and a host of other things, which is why I gave up on using namespace ages ago.

    The idea of restricting scope is a good one, but frankly objects can do just as good a job if you have a clue what you are doing. The only legitimate reason I can see for using namespaces in PHP seems to be as a proof against developer ineptitude -- making it so when people just blindly cut and paste together code from different sources, your code doesn't conflict in that environment.

    ... and after over three and a half decades of programming, that just doesn't make any sense at all.

    Do yourself a favor, and lose the namespace nonsense.
    Java is to JavaScript as Ham is to Hamburger.

  5. #5
    Join Date
    Feb 2008
    Posts
    19
    Oops - forgot myself

    PHP Code:
     <?php namespace main\application\controllers;

    class 
    Controller
    {
        public function 
    load()
        {
            
    $pageController 'defaultController';

            
    $nsPageController "\\main\\application\\controllers\\" $pageController;
            
    $page = new $nsPageController;
            
            
    $page->test();
        }
    //!EOF : /main/application/controllers/controller.php
    This now works.

    Changes: forgot to escape backslashes and completely forgot the 'new' keyword doh!

    My problem seems to stem from the use of the correct namespace regardless of it having the same namespace.

    Thanks for the help NogDog

  6. #6
    Join Date
    May 2014
    Posts
    905
    Quote Originally Posted by DAL. View Post
    Changes: forgot to escape backslashes and completely forgot the 'new' keyword doh!
    Better trick for that? Don't use double quotes on strings when you aren't using the parsing. It's hair faster too (since the parser doesn't have to check for as much stuff)

    As a rule, I also don't use double quotes in PHP anymore unless I REALLY need a C-style escape sequence. The behavior is too sloppy and unpredictable.
    Java is to JavaScript as Ham is to Hamburger.

  7. #7
    Join Date
    Feb 2008
    Posts
    19
    Quote Originally Posted by deathshadow View Post
    In my experience PHP namespaces are a disaster... I've never had anything behave as expected when trying to use them; they outright break variable variables, variables as class names, and a host of other things, which is why I gave up on using namespace ages ago.

    The idea of restricting scope is a good one, but frankly objects can do just as good a job if you have a clue what you are doing. The only legitimate reason I can see for using namespaces in PHP seems to be as a proof against developer ineptitude -- making it so when people just blindly cut and paste together code from different sources, your code doesn't conflict in that environment.

    ... and after over three and a half decades of programming, that just doesn't make any sense at all.

    Do yourself a favor, and lose the namespace nonsense.
    Sounds like a very valid point! I'm with you on this.

  8. #8
    Join Date
    Feb 2008
    Posts
    19
    Quote Originally Posted by deathshadow View Post
    Better trick for that? Don't use double quotes on strings when you aren't using the parsing. It's hair faster too (since the parser doesn't have to check for as much stuff)

    As a rule, I also don't use double quotes in PHP anymore unless I REALLY need a C-style escape sequence. The behavior is too sloppy and unpredictable.
    I would normally do this but in my effort to pulling everything back to simplicity I was concentrating on getting the logic to work. It's only after getting the solution that I realised I've got a Sublime Text 2 issue. The reason why I missed the original "\\" in the first place was that my highlighter is screwed up.

    Here's a screen shot of Sublime editor in action. Something somewhere confused it.

    example1.jpg

    example2.jpg

    [EDIT] actually I've just made a mistake on these examples. I guess it shows how much I'm in tune with the highlighter, once that screws up I'm pretty random . The example still shows that the editor highlighter is not working correctly.

    Anyone else had issues with this kind of thing?

    I'm new to sublime, I used to use SEG which was a really basic notepad with highlighter but excellent. I've needed to use something with a bit more of a project view and liked the look of sublime.
    Last edited by DAL.; 05-31-2014 at 07:45 AM.

  9. #9
    Join Date
    May 2014
    Posts
    905
    Quote Originally Posted by DAL. View Post
    was that my highlighter is screwed up.
    That's actually a good chunk of why I don't use that either. Relying on a tool to catch one's own mistakes makes one get in the habit of not taking the time to catch your own mistake.

    The other part being I find the acid trip of constantly changing colors completely illegible... I've never quite grasped how colour syntax highlighting type tools are actually useful - just makes things harder for me. I hated it in the '80's when I first encountered it in Turbo Pascal 4, and I've seen no real improvements to it to make it actually useful since.
    Java is to JavaScript as Ham is to Hamburger.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles