SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class name must be a valid object or a string

    I want to be able to do the following:

    PHP Code:
    $this->recordClassName 'Blah';
    return new 
    $this->recordClassName($this,$this->data); 
    I was rather hoping that doing the above would be the equivalent of saying:

    PHP Code:
    return new Blah($this,$this->data); 
    But it returns the following error:
    Code:
    Fatal error:  Class name must be a valid object or a string in [redacted]
    How do I get PHP to recognise recordClassName as a string?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't get a problem here:
    PHP Code:
    <?php
    class Test{}
    class 
    Init{
        public 
    $classname;
        function 
    __construct(){
            
    $this->classname "Test";
        }
        function 
    output(){
            
    var_dump(new $this->classname());
        }
    }
    $init = new Init();
    $init->output();
    What is output when you echo $this->recordClassName?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Argh, to cut a long story short I was defining the variable after calling parent::__construct() which is why it wasn't working.

    Thanks!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Reflection, you should look into it.
    PHP Code:
    <?php

    class Testing
    {
        public function 
    something $class )
        {
            
    $rc = new ReflectionClass$class );
            return 
    $rc->newInstance$this$this->data );
        }
    }

    $t = new Testing;
    $c $t->something'Blah' );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •