tags:

views:

24

answers:

4

i have a multipage form with some textfields. When you fill out the form and press next the form stores the values in an object. When you press the backbutton it reloads those values in textfields. This works fine.

But when you initially load the form, the object isn't created so there is nothing to load the values from and i get a Call to a member function on a non-object error.

example:

<inputfield value='$object->getValue()'>

is there a way to tell it that when the object doesn't exist to just leave it empty?

A: 

There is is_object method:

if (is_object($object)){
  // it is there
}

So you can check for it something like this:

<inputfield value='<?php is_object(@$object) ? $object->getValue() : '';?>'>
Sarfraz
+3  A: 

You could do the following before using the object:

// All methods called on this object return an empty string.
class EmptyObject {
    public function __call($name, $args) {
        return '';
    }
}

// Use fake object if $object is not defined correctly.
if (!is_object($object)) {
    $object = new EmptyObject();
}

__call is a magic method in PHP. It gets invoked every time an undefined method is called on the object.

elusive
so far it's working, nice trick!
Dazz
+1  A: 

The is also an isset method:

if(isset($object)){
     //it is there
}
Orbit
+1  A: 

Try this:

$output = "<inputfield value='". (isset($object) ? $object->getValue() : '') . "'>";
Spudley