0

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?

1
  • Can you show all code? Where is your object initialization? Commented Oct 28, 2010 at 12:40

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

The is also an isset method:

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

Comments

1

Try this:

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

Comments

0

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() : '';?>'>

Comments

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.