Jump to content

Undefined Class Properties


ShadowMage

Recommended Posts

Trying to understand something here. The following code:

class Test {    public $arrTest;    public function __construct() {        $this->arrTest = array("one");    }    public function toString() {        $string = $this->arrTest[0];        echo $string;    }}$test = new Test();echo $test->toString();

Runs perfectly fine. No errors. Outputs "one" just as I'd expect.

If I change the __construct function as follows:

$this->arrTest = array();

I get an error saying there's an undefined index. Just as I would expect.

And if I try to access a property that doesn't exist, say:

$string = $this->unknownProperty;

I get an error trying to access an undefined property. Just as I would expect.

 

Here's where I don't quite understand what's going on:

class Test {    public $arrTest;    public function toString() {        $string = $this->arrTest[0];        echo $string;    }}$test = new Test();echo $test->toString();

This code produces nothing. No errors, no output. Shouldn't this generate some kind of error? Undefined index or property or something? (Error reporting is set to E_All)

Edited by ShadowMage
Link to comment
Share on other sites

Hmm, that might be a weird edge case. In PHP you can also use array syntax to refer to individual characters in a string. What PHP might be doing there is referring to the first part of an empty string, which itself is an empty string. I imagine if you do var_dump on $string it will say a string of length 0. It would also be interesting to see what happens if you initialize arrTest as an empty string.Also, you don't need 2 echos. toString does not return anything, there's no reason to echo the return value.

Link to comment
Share on other sites

Hmm, that might be a weird edge case. In PHP you can also use array syntax to refer to individual characters in a string. What PHP might be doing there is referring to the first part of an empty string, which itself is an empty string. I imagine if you do var_dump on $string it will say a string of length 0. It would also be interesting to see what happens if you initialize arrTest as an empty string.Also, you don't need 2 echos. toString does not return anything, there's no reason to echo the return value.

var_dump on $string returns NULL. If I initialize arrTest as an empty string, it throws an "Uninitialized string offset" error.

 

As for the echos...that was an oops. I wrote the code in about 2 seconds and my fingers just automatically typed it...

Link to comment
Share on other sites

It looks like they've left that undefined. It might happen with an int also:

$var = 1;echo $var[0];
I believe the reason for that is a line on this page:

Note:The behaviour of an automatic conversion to array is currently undefined.

Maybe it would have been nice for them to define an automatic conversion to an array as a notice or warning, but there may be some use case somewhere where an array is expected but they don't bother to check the type and don't want to throw an error if it's not an array.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...