Object Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • empiresolutions
    New Member
    • Apr 2006
    • 162

    Object Error

    I have just recently updated my server to PHP 5.1. I am getting a grip of errors in my logs now related to an object i use. This is just one of the error.

    Code:
    Undefined property:  object::$core in /var/www/vhosts/site.com/httpdocs/index.php
    It is referencing a script i use like -

    Code:
    // set object //
    class object {};
    $SITE = new object;
    
    // difine //
    $SITE->core = 'test string';
    
    // in use //
    echo"$SITE->core";
    Has this type of object usage degraded in 5.1, or is there something i need to set in my now updated php.ini file.

    Thanks.
  • nathj
    Recognized Expert Contributor
    • May 2007
    • 937

    #2
    I know this an old post and the issue has probably been sorted but I'm just trying to help out.

    Looking at the code sample you haven't said that core is property of the class. try:

    Code:
    <?php
    class myObject
    {
     var $core ;
    }
    
    $site = new myObject() ;
    
    $site->core = "hello world" ;
    
    echo $site->core ;
    ?>
    Cheers
    nathj

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      It makes sense that you can't access properties a class doesn't have. No?

      Anyway, in the CodeIgniter framework, you can create properties on the fly. That's done like:

      Code:
      // 'new_property' doesn't exist until:
      $this->new_property = "A new property";
      I'm not quite sure how they do that, but someone else might.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Actually, in PHP 5.2.0, creating properties on the fly works fine. I was unaware of that ability..

        Code:
        class Test { };
        
        $test = new Test;
        
        $test->new_property = "testing";
        
        echo $test->new_property;
        Works just fine for me, but, like I said, that is 5.2.0

        Comment

        Working...