The content of an object is changed after executing a function

There are two objects of the same class:

$obj1 = new MyClass();
$obj2 = new MyClass();

Then I applied a function func to $obj2. However, once this function is executed, I noticed that the content of $obj1 has magically changed as well. I do not use any reference to $obj1 inside func. What might be the possible reasons of this problem? How do I solve it?

$obj2 = func($obj2,$vars);

I should say that I use other functions before func, and they also use $obj2 as an input. However, after their execution the content of $obj1 get not changed.

P.S. When I debug this code in Zend Studio and go inside func, Watch list Expressions says that $obj1 = null, but I think it's fine, because func is saved in a different PHP file.

Answers


Consider this:

class MyClass
{
  public static $value;

  public function test( $testvalue )
  {
    self::value = $testvalue;
  }
}

$obj1 = new MyClass();
$obj2 = new MyClass();

$obj1->test( 123 );

echo $obj2::value; // this will echo 123!!

There are no changes to made to $obj2, only to $obj1. The static causes the var to loose its object contents, and becomes 'global' to all objects.


Need Your Help

Manage database connection using connection pool or session. How?

database session servlets connection-pooling

I have a simple task to accomplish, but I am not sure what is the best way to go by.

Build Runtime License option missing in VS 2010

visual-studio-2010 sitecore

I am attempting to build a runtime license for a third party component for ASP.NET. The instructions are to place the "licenses.licx" file in the root folder of the site. The file is also embedded ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.