0

Because I'm a programmer who likes clear, concise and less error-prone code, I always assign my variables a default value before they are used; I'm sure we all know why this is a good idea.

The one case that I find extremely annoying however, is when it comes to pre-setting variables that are meant to hold objects. I haven't found a suitable initial value worth setting.

I find it important to point out that I'm also not a fan of relying on PHP's loosely typed features. I do not like changing the type of a variable during it's lifespan, as I find it to be a messy, error prone guessing game. In the past, I have set my object variables initial value to NULL, but this isn't really an appropriate value in PHP as this removes the variable from the identifier table and allows it to be collected by GC; odds are it wont be collected before it's used, but it still seems dirty.

That said, what do you guys set your initial values to be when your variable holds an object?

$foo_id  = 0;
$is_foo  = false;
$foo     = '';
$obj_foo = NULL; // not really an appropriate initial value

Edit

For clarification as to why I think the default value should not be set to null, take the following:

$obj_foo = null;
echo isset($obj_foo); // false

Often, this wouldn't cause a problem, but I could see it doing so for that painful 1%.

4
  • How do you determine which answer to accept, when you are just asking how people do it? Should be a CW then. Commented Nov 8, 2010 at 19:10
  • NULL is not that bad, at least it's better than an undefined variable. Commented Nov 8, 2010 at 19:14
  • @Gordon That's true, it should be. I didn't think about it when I was posting the question. I'm new around here and am not sure if it can be changed now :/ Commented Nov 8, 2010 at 19:18
  • flag for Moderator attention and ask them to make it CW then. Commented Nov 8, 2010 at 19:21

2 Answers 2

3

I do not set any value in that case. They will be NULL without explicit assignment anyway.

class Foo
{
    /**
     * @var SomeClass
     */
    protected $_object;

    public function __construct()
    {
        $this->_object = new SomeClass;
    }
}

Using NULL is perfectly fine, because as the Manual states

The special NULL value represents a variable with no value.


Since you mentioned setting a value to NULL has the same effect as using unset on it, please consider the UnitTest below. Setting a variable to NULL will not destroy it and it wont be collected by the GC. Using unset will destroy a variable and remove it immediately.

class NullTest extends PHPUnit_Framework_TestCase
{
    public function testSettingNullDoesNotRemoveVariable()
    {
        $foo = NULL;
        gc_collect_cycles(); // forcing GC to collect
        $this->assertNull($foo);
        $this->assertArrayHasKey('foo', get_defined_vars());
    }
    public function testUnsetRemovesVariable()
    {
        $foo = NULL;
        unset($foo);
        $this->assertNull($foo); // raises Notice "Undefined variable: foo"
        $this->assertArrayNotHasKey('foo', get_defined_vars());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

By myself I prefer to use null and phpdoc comments, it also useful for ide autocomplete and auto-generating documentation:

/*
  @var Class
*/
$obj = null;

But if you really want to have something that represents object you may try to create stdClass instance.

$obj = new stdClass();

UPD: Why do you think that null isn't appropriate? It was invented to represent nothing. You shouldn't make your code complicated just because it "OO-design" or some weird methodology.

4 Comments

I thought about stdClass too but your object will evaluate to true. So it isn't feasible.
I think null isn't appropriate because in PHP it has the same effect as unset()ing a variable, which is the exact opposite of what you are trying to accomplish when setting an initial value.
See additional example above regarding isset().
@Cragie: No you are not right. NULL constant is not the same as NULL byte. php.net/manual/en/function.isset.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.