Does setting $this = &$otherObject change anything? - php

The comments indicate that I have to clarify my point: I'm a developer and consultant working on foreign code here. My current assignment is to help migrating an existing (very large) system from PHP4 to Java. While reviewing the code I stumbled across a piece of code that quite confuses me.
Some class (let's call it TestClass) in the system I'm working on does something like this:
function reload(){
$newobject = new TestClass();
$this = $newobject;
}
I gave it a quick try, expecting some inconsistent behavior between accessing members of the object directly or via some getter in case this somehow works. I thought that at worst the external pointers to the existing object and the internal one (aka $this) would target different portions of the heap.
Quick tests seem to indicate that simply nothing happens.
Any idea if this has any (side) effect whatsoever?
Here is my complete test:
<?php
ini_set('display_errors', true);
class TestClass{
var $var1;
function TestClass(){
$this->var1 = 0;
}
function getVar1(){
return $this->var1;
}
function setVar1($value){
$this->var1 = $value;
}
function reload(){
$newobject = new TestClass();
$this = &$newobject;
}
}
echo "<h3>Creating TestObject</h3>";
$testObject = new TestClass();
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
echo "Setting member directly to 1<br>";
$testObject->var1 = 1;
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
echo "<h3>Calling reload</h3>";
$testObject->reload();
echo "Accessing member directly: " . $testObject->var1 . "<br>";
echo "Accessing member via get: " . $testObject->getVar1() . "<br>";
?>
I expected to get 1 and 0 at last two calls if $this now pointed towards the new object while $testObject would still point to the original one, but 1 is returned in both cases. So I need to know whether the reassignment of $this is doing anything but being a bad idea. If not I can dump that part for good. So if anyone knows of any side effects please tell.
PS: I am fully aware that the above code is not using visibility and such. The original system is written in PHP4 so why bother :-|

Okay, with the hint of a colleague I got it. If the reload function is changed to
function reload(){
$newobject = new TestClass();
$this = $newobject;
}
the object is overwritten with the content of the newly created one. This is due to the copy on write mentioned by "i put on my robe an wizard hat" in the comment above. If used with the forced call-by-reference nothing happens which is good.

Related

Accessing a member of a class using a string variable with the name of the member

Short Version: Why can't we access a function like this:
$b = "simple_print()";
$obj->$b;
Complete Version:
Suppose we have a class User defined like this:
class User {
public $name;
function simple_print() {
echo "Just Printing" . "<br>";
}
}
Now if a create an User object and set the name of it we can print its name using
$obj = new User;
$obj->name = "John";
echo $obj->name;
Although it is strange we also can do something like this in order to print "John":
$a = "name";
echo $obj->$a;
But we can't access a function using the same idea:
$b = "simple_print()";
$obj->$b;
Why? Shouldn't it work the same way?
Also, does anyone know what is it called? I tried to look for "accessing a member through a variable" and "using a method through a variable with the name of it" but I didn't find anything related to this.
Extra info: The version of PHP I'm using is: PHP version: 5.5.9-1ubuntu4.7
You were very close, but made a small logical mistake. Try this instead:
$b = 'simple_print';
$obj->$b();
This is because the method is accessed by it's name, which is simple_print, not simple_print(). The execution is triggered by the parenthesis, but that is not part of the name, so of how you access the method.
Here is a short example:
<?php
class Test
{
public function simple_print() {
echo "Hello world!\n";
}
}
$object = new Test;
$method = 'simple_print';
$object->$method();
As expected it creates the output Hello world! if executed on CLI.

Why is this PHPUnit comparison wrong? ( .. $mock->with($this->identicalTo($foo) )

I have a problem with PhpUnit. I want to use the isIdentical method of PHPUnit to ensure that a method is called with a specific object as a parameter.
Given is a mock object with method "setBar":
$bar = new Bar();
$someMock
->expects($this->any())
->method('setBar')
->with($this->identicalTo($bar));
$someMock->setBar($bar);
This is of course only an example. It should work, but it doesn't. To get some hints, i wrote some echo code in the PHPUnit_Framework_Constraint_IsIdentical method:
public function evaluate($other, $description = '', $returnResult = FALSE)
{
//...
echo "\n";
echo spl_object_hash($other) . "/". spl_object_hash($this->value). " - ". get_class($this->value) ."/". get_class($other);
echo " ->> ";
var_dump($other === $this->value);
}
The object hash of $other is not the same as $this->value (while the hash of $this->value is actually the correct one)
In the meantime, i found the reason for this error here. It's an issue of PHPUnit. Objects getting cloned by PHPUnit. I leave the question If someone knows a good workaround for this issue.
https://github.com/sebastianbergmann/phpunit-mock-objects/issues/41

Do all variables in methods need to be object properties? (PHP)

I'm learning OO PHP and am trying to get some of the coding practices straight. Here is a pared down version of some code I'm using for error (and exception) handling:
final class MyErrorExceptionHandler {
private $level = array(); // error levels to be handled as standard errors
private $path = array(); // full path to file
private $path_short; // filename plus working dir
public function myErrorHandler($severity, $message, $file, $line) {
if (error_reporting() & $severity) { // error code is included in error_reporting
$this->level = array(E_WARNING => 'warning',
E_NOTICE => 'notice',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');
if (array_key_exists($severity, $this->level)) { // handle as standard error
/*$this->severity = $severity;
$this->message = $message;
$this->file = $file;
$this->line = $line;*/
$this->printMessage($severity, $message, $file, $line);
} else { // fatal: E_USER_ERROR or E_RECOVERABLE_ERROR use php's ErrorException converter
throw new ErrorException($message, 0, $severity, $file, $line);
}
}
} // fn myErrorHandler
private function printMessage($severity, $message, $file, $line) {
echo ucfirst($this->level[$severity]) . ': ' . $message;
$this->shortenPath($file);
echo ' in ' . $this->path_short . ' on line ' . $line;
} // fn printMessage
private function shortenPath($file) {
$this->path_short = $file;
$this->path = explode(DIRECTORY_SEPARATOR, $file);
if (count($this->path) > 2) { // shorten path to one dir, if more than one dir
$this->path_short = array_pop($this->path); // filename
$this->path_short = end($this->path) . DIRECTORY_SEPARATOR . $this->path_short; // dir+file
}
} // fn shortenPath
} // cl MyErrorExceptionHandler
The title of this question is probably a little bit off because I'm not 100% on the terminology. Basically I'm trying to figure out a few things.
Is it right to explicitly declare $level and $path as arrays?
Should $level be declared as it is (and made $this->level)? If so, have I assigned its value (E_WARNING etc.) in a wise place? Would the constructor (not shown here) be a smarter choice?
Note the commented block in myErrorHandler(). Originally I had declared all of these properties at the top of the class, and then called $this->printMessage() without any parameters. Which is the more correct way? If I keep the code as is, would I want to then use $this->severity = $severity etc. inside printMessage()?
So, would it be better to:
replace
$this->shortenPath($file);
echo ' in ' . $this->path_short . ' on line ' . $line;
with
$path_short = $this->shortenPath($file);
echo ' in ' . $path_short . ' on line ' . $line;
ultimately, and give a return value in shortenPath()?
I realize this is a mishmash of several different questions, but what I'm trying to get at is a common inquiry about the proper style of declaring/using variables/properties, specifically when dealing with methods.
To summarize: When should I use $this->foo = $foo?
EDIT: sorry, I have assumed below that you would create a new instance of the 'object' with each error which obviously you are not doing. Just edited my answer to reflect this.
"When should I use $this->foo = $foo?"
There can be several cases in which you would do this, but it's usually if you create $foo within a method and wish to have that then accessed by the entire object.
For example, if you wanted to call on an object and use that within this particular object (if it doesn't make sense to extend). You would do something like:
$foo = new DataModel();
$this->foo = $foo;
OR
$this->foo = new DataModel();
That object may be a decorator or something else related to error handling and the above code would usually feature in your constructor. You could then access the methods of that object any time by using:
$this->foo->objectMethod();
..and to express something noted in the comments to this answer:
"would you assign $file to the object as that is used in several methods?"
I wouldn't assign $file to the object,
here's why. The semantics of the word
"property" means "belongs to". In your
case, your class is a error handler.
$file doesn't belong to the error
handler, it belongs to an error
instance. If your class was
MyErrorHandler_Error (created for each
instance of a triggered error), then
$file would be a property of that
class, along with $line and $level.
To answer what I can from your other questions:
It's neither. I would consider it preference.
Yes - any variables or values which should be available to your entire object and required for the object to run properly, should probably be set within your constructor, if not within your variable declarations (not sure of terminology there) at the top of the class.
read the comments below. Because this particular class deals with multiple instances of errors - assigning the properties of those errors to the object wouldn't be best practice as you will be overwriting them with each new error. However, it does make sense to store all of your errors and error properties within an array assigned to the object if you require to access historical data. For example, at the moment, if you create a new error - that is all you are doing. You have no way of accessing any old errors this object has created.
see above
You should also think about conflicts when assigning properties to objects. Are you likely to reassign, because if so, the old property will be gone. Fairly simple but still something you have to consider.

Zend Framework view script, functions and variable scope

I am rather new to ZendFramework and am trying to figure this out. In my view script (index.phtml), I have this bit of code that says:
<?php
function getErrorString($element)
{
echo "<pre>";
print_r($this);
echo "</pre>";
$string = '';
if(!empty($this->error[$element]))
{
$string = $string.'<label class="error" for="'.$element.'" generated="true">';
foreach($this->error[$element] as $error)
{
$string = $string.$error;
}
$string = $string.'</label>';
}
return $string;
}
echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString("blah");
die();
That gives me:
Fatal error: Using $this when not in object context in index.phtml on line XX
It seems to me that when you create a function within a view, you lose the $this variable. I did search around the net, and I can't see anyone else trying to achieve what I am doing (highly unlikely, maybe I'm searching it wrong).
With past experience developing other apps, I can't see a good reason why this function should be placed in a separate helper -> especially since this is the only place the function will ever be called.
Any ideas would be greatly appreciated.
Your function getErrorString() isn't an objectmethod of the Zend_View-Object.
It has it's own scope and couldn't reach $this.
The following code should work for you in the index.phtml
function getErrorString($viewObject, $element)
{
echo "<pre>";
print_r($viewObject);
echo "</pre>";
$string = '';
if(!empty($viewObject->error[$element]))
{
$string = $string.'<label class="error" for="'.$element.'" generated="true">';
foreach($viewObject->error[$element] as $error)
{
$string = $string.$error;
}
$string = $string.'</label>';
}
return $string;
}
echo "<pre>";
print_r($this);
echo "</pre>";
getErrorString($this,"blah");
die();
The last use of "$this" variable is probably the main reason for showing the fatal error. It's quite justified because of the fact that you cannot write anything else in the class definition, except defining methods & properties with respect to that class.
Also if you are creating any function in a view page, then within that function the "$this" variable is not accessible by default. So you will have to make that "$this" variable go global or you need to print the required part, related to "$this" variable, outside the function definition.
echo "<pre>";
print_r($this);
echo "</pre>";
So when you are writing the above code in the function definition, the PHP Parser is unable to find any object context for this "$this" variable. It's not that you are losing that "$this" variable, but it will not be accessible, but to the missing logic.
Hope it helps.

Possible Multi-Instanced Object Problem in PHP

I'm getting a "Call to a member function process_data() on a non-object in page.class.php on line 35" even though the object has been called.
Here is the index.php extraction showing the object being instantised
// require our common files
require("modules/module.php");
require("registry/objects/datetime.class.php");
require("registry/objects/page.class.php");
// load in all the objects
$datetime = new dateandtime;
$page = new page;
$module = new module;
It then passes to the Process class
require("template.class.php");
$template = new template($php_path . "controllers/themes/adm/" . $page . ".html");
// Place in both commonly used language and page specific language
$template->language($php_path . "controllers/language/en/adm/common.php");
$template->language($php_path . "controllers/language/en/adm/" . $page . ".php");
// Tell the page's module to load in data it needs
$module->process_data("module_" . $page);
// Output the final result
$template->output();
It's at this point PHP is throwing the error. The contents of the module.php file is as follows
class module {
public function process_data ($child) {
require($child . ".php");
read_data();
return true;
}
}
I've tried moving the instance declaration to within the second pasted code, but that generates more errors, because the class that "module" calls in then uses some of the "template" classes as well - so the same issue occurs just further down the line.
What am I getting wrong her, or completely missing, I'm sure it's the latter but I really need help here. Thanks
It looks to me as if variable $module was not in the scope when you try to call object method. Could you try var_dump($module) before $module->process_data("module_" . $page). What is the result of this function? Quick solution may be declaring $module global, but globals are not a very good idea anyway (but you may check if it works).

Categories