php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46401 String concatenation with class attribute
Submitted: 2008-10-27 13:17 UTC Modified: 2008-10-27 14:18 UTC
From: victor at equillon dot ro Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: victor at equillon dot ro
New email:
PHP Version: OS:

 

 [2008-10-27 13:17 UTC] victor at equillon dot ro
Description:
------------
When concatenating a string with an attribute of a class, the attribute is considered to be void or something. It doesn't always run like that.

It can be fixed by doing this:

echo "Monster ".($this->monsters[0]->name)." hit something";

Reproduce code:
---------------
class Monster {
   public $name;
}

class test {
   private $monsters; 
   
   public function init() {
      for ($i=0; $i<100; $i++) {
         $this->monsters[$i] = new Monster();
         $this->monsters[$i]->name = "test";
      }
   }

   public function test() {
      echo "Monster ".$this->monsters[0]->name." hit something";
   }
}

$abc = new test();
$abc->init();
$abc->test();

Expected result:
----------------
Monster test hit something

Actual result:
--------------
Monster hit something

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-27 13:43 UTC] jani@php.net
I suggest you turn on full error_reporting on your development machine:

# php -derror_reporting=E_ALL -n t.php

Notice: Trying to get property of non-object in /home/jani/t.php on line 18

And then read about constructors and classnames..
 [2008-10-27 14:18 UTC] victor at equillon dot ro
I apologise, I wasn't paying much attention. You can close this "bug".
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 28 10:00:03 2025 UTC