Re: [PHP] accessing variables within objects



Philip Thompson wrote:
On Jul 30, 2008, at 1:29 PM, Jim Lucas wrote:

Marten Lehmann wrote:
Hello,
I'm using some php-classes which worked fine with php-5.0.4. Now I tried to upgrade to php-5.2.6, but the classes give a lot of errors. If I set
error_reporting(E_ALL);
I see messages like
Notice: Undefined property: FastTemplate::$main in /whereever/inc.template.php on line 293
Notice: Undefined property: current_session::$cust_id in /whereever/inc.init.php on line 117
In inc.template.php there are a lot of calls like $this->$key. In inc.init.php there are calls like $session->cust_id.

to fix these errors, you would need to modify the code so it does something like this.

where it calls $this->$key you need to check and make sure that $key exists before you trying call for it.

So something like this would work.

if ( isset( $this->$key ) ) {
$this->$key;
} else {
$this->$key = null;
}

You didn't show any context in which you are using the above code. So I don't know what will actually work in your situation. Show a little more code that includes the method in which $this->$key is called.

You will want to look at using the Overloading feature of PHP5. Check out this page for overloading examples

http://us2.php.net/manual/en/language.oop5.overloading.php

Take note of the __get() and __set() methods. The __get method checks to see if the key exists before it tries working with it.

Ok, I'm trying to understand the point to using these overloading methods.

<?php
$obj = new ClassThatUsesOverloading ();
$obj->hi = 'Hi';
$obj->bye = 'Bye';

echo $obj->hi, ' ', $obj->bye;
// Output: Hi Bye
?>

I was only suggesting the overloading because it would be the simplest thing to implement in an existing class without massively re-write parts of your application.

You could simply add the __get() method and then have it check to see if the value or $key existed before it tries using it. And if it doesn't exist, then return null or something else. This would get rid of a lot of your NOTICEs from PHP.

I would add a method something like the following to your class and it should fix the problems with trying to access properties that do not exist.

function __get($key) {
// Check for the existence of the key in object
if ( isset( $this->$key ) {
// If found, return value
return $this->$key;
}
// Default: return null
return null;
}



You could have done that or you could do the following.....

<?php
$obj = new ClassThatDoesntUseOverloading ();
$obj->setHi('Hello');
$obj->setBye('Bye Bye!');

echo $obj->hi(), ' ', $obj->bye();
// Output: Hello Bye Bye!
?>

This example is flawed by the fact that you are call hi and bye as a method instead of referencing the property.


The 2nd way seems more *OOP* than the first - weird to explain. I guess what I'm wanting to know is.... why would you use overloading (in PHP)? The only reason I can think of is to avoid having to create/use accessors. Please help me understand! But please be nice! =D

Thanks,
~Philip


What has changed in php-5.2.x so that these calls don't work any more? What is the new, required form to use objects in a similar manner (unfortunately I have no ressources to code these classes from scratch)? Thanks.
Kind regards
Marten



--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

.



Relevant Pages

  • Re: [PHP] accessing variables within objects
    ... Here's the PHP doc page. ... Philip Thompson wrote: ... You will want to look at using the Overloading feature of PHP5. ...
    (php.general)
  • Re: Object of class Person could not be converted to string
    ... the language had been better planned from the start. ... Implement operator overloading in its entirety and allow the base ... of PHP 5, __toStringmethod is used if applicable. ... variable type in a string context, you can no longer do that. ...
    (comp.lang.php)
  • Re: [PHP] accessing variables within objects
    ... You will want to look at using the Overloading feature of PHP5. ... This would get rid of a lot of your NOTICEs from PHP. ... I would add a method something like the following to your class and it should fix the problems with trying to access properties that do not exist. ...
    (php.general)
  • Re: Why no overloading in PHP5?
    ... >> Given that PHP isn't strongly type, I don't really see the need. ... Overloading allows you to do things ... PHP, the answer is at least twofold. ... typing strength of a language is extremely important here. ...
    (comp.lang.php)
  • Re: Why no overloading in PHP5?
    ... >> Given that PHP isn't strongly type, I don't really see the need. ... Overloading allows you to do things ... PHP, the answer is at least twofold. ... typing strength of a language is extremely important here. ...
    (alt.php)