Unfortunately Google doesn't like to include funny characters such as '$' in search terms, so searching for fun things like '$this' doesn't turn up much that's useful.
I'm working on a PHP project that leverages object inheritance (derived classes), objects which instantiate other objects as variable members of themselves, including instantiating objects with variable class names. All in all, things have been moving along pretty well and I feel like I've gotten the hang for OOP coding for PHP. But just when I think I've got things figured out, something mysterious has popped up. Because the code is for a commercial application, I can't release it, so I can only describe the problem and hope it makes sense to someone here and that the solution is a simple one. I've tried and FAILED to recreate the problem with a simpler, short script - there's something about my big pile of code (actually it's only about 1000 lines in all including user interface HTML) that is making it break.
Basically what seems to be happening is the special $this variable/operator/whatever the heck it's considered seems to be altered at some point from it's normal behavior. Normally if I am within a method (function) of my object (class) and I reference $this->member_variable_name I can set the value of that variable, retrieve it, and access the same variable from other methods as if it were any normal data member of an object as you might expect. The behavior that I am seeing is that if I instantiate $this->$person['1'] = 'me'; from the constructor and then return to the caller and later the caller calls some other member function, the member function can read the value of $this->person['me'], but any changes that it makes to $this->person including modifications to the 'me' entry, or addition of other entries are restricted to being visible from ONLY within that member function. Once control is returned to the caller and we look at the values of the object's $person member variable, none of the changes are kept.
It seems like a strange scope problem that allows me to READ the values, but writes make some sort of temporary new, mirrored structure that gets dropped without writing back to the original location. Is this a scope problem? Is it a problem with the way my member variables are defined? I'm using what I believe to be quite common coding practices, so I'm just at a loss at this point.
I wrote the following script to try to duplicate the problem. The hierarchy of calls matches exactly what my main application does, so I thought perhaps the results would be the same. Indeed it would seem it's not that easy as the script returns the output that you would expect it to with the final values clearly being different from the initial values. With my main application however, the result is that the final values are equal to the initial values:
<?php
class worker {
var $people = array();
function worker() {
$this->init();
}
function init() {
$this->people['dave'] = '1';
}
function display() {
foreach ($this->people as $name => $value) {
print $name . '=[' . $value . "]\n";
}
}
function modify() {
$this->people['pete'] = '2';
$this->modify2();
}
function modify2() {
$this->people['sean'] = '3';
$this->modify3();
}
function modify3() {
$this->people['bobby'] = '4';
}
}
$coworkers = new worker();
print "Initial Values\n";
$coworkers->display();
print "\n\n";
print "Modifying...\n";
$coworkers->modify();
print "\n\n";
print "Final Values\n";
$coworkers->display();
print "\n\n";
?>
I would like to preempt a probable response by disclaiming that this is not a problem with dropping the $this from some references in my main application's code and generating a local variable instead of accessing the main member variable. Not only am I quite careful about that, but a local $people would not be instantiated with a copy of the value from the $this->people - it would start out empty. That is not what's happening.
I have to guess that something else within my code is changing the behavior or meaning of $this because once the constructor's done, it seems member functions can only read from and not write to and $this->member_variable_name.
Hopefully this rings some bells with someone. I've been pulling my hair out all day over this one - any tips/guidance would be most appreciated.
Thanks,
- Sean Kelly