2

Say I have a simple class and I create it and call a function on it like this:

class tst
{
  private $s = "";

  public function __construct( $s )
  {
    $this->s = $s;
  }

  public function show()
  {
    return $this->s;
  }
}

$t = new tst( "hello world" );
echo "showing " . $t->show() . "\n";

Is there any syntax or workaround that will allow me to instantiate an instance of tst and call the show() function without assigning the object to a variable? I want to do something like:

echo new tst( "again" )->show();

I don't want to declare my functions as static as I want to use them in both of the above examples.

5
  • What advantage does tst::create(x)->show() over just using x?!? Or f(x) if there's actually some work (validation, normalization, conversion) ones in the constructor? Commented Apr 4, 2011 at 15:40
  • @delnan: Your question is equivalent to asking "why use classes in programming?". A more relevant question (but one which I feel is adequately answered by the OP) is "why not make the methods static?". Commented Apr 4, 2011 at 15:48
  • @Jon: Rest assured that I know, understand and appreciate OOP. But wrapping something in this class, which does nothing, and then retrieving the wrapped value instantly, seems pretty useless. The equivalent of (function (x) { return x; })(x) in JS, if you will. And even if the constructor did something useful with that value, there's no need to wrap the result in an extra object. Commented Apr 4, 2011 at 15:50
  • 1
    @delnan: I see, and of course I agree with you. But I believe the OP is just giving us an example here; that's probably not what the real class does. Commented Apr 4, 2011 at 15:59
  • @delnan - I take your point, the example does nothing useful. It was the simplest code to illustrate what I wanted to do and my real code does not have simple show() functions but others that actually manipulate the variable passed in the constructor Commented Apr 4, 2011 at 16:00

3 Answers 3

3

You can't do what you want exactly, but there are workarounds without making things static.

You can make a function that returns the new object

function tst( $s ) {
    return new tst( $s );
}

echo tst( "again" )->show();
Sign up to request clarification or add additional context in comments.

Comments

3

To answer your question:

public static function create( $s )
{
    return new tst($s);
}

public function show()
{
    return $this->s;
}

The above will allow you to do tst::create("again")->show(). You can rename create as you like.

2 Comments

Thanks for the answer. I accepted Galen's as it results in slightly cleaner syntax.
@SteveClaridge: Regardless of the accept, I feel that having an extra free function (with a parameter list that needs to be kept in sync with the class constructor) in order to save 5 keystrokes is not the best approach. But it's your code, your decision.
1

Agile Toolkit uses this approach everywhere. It uses add() method wrapper which is defined for global object ancestor. Here is some real-life code:

$page
    ->add('CRUD')
    ->setModel('User')
    ->setMasterField('admin',false);

This code creates 'CRUD' view, puts it on the page, creates and links with Model_User class instance which receives additional condition and default value for boolean 'admin' field.

It will display a CRUD control on the page with add/edit/delete allowing to edit all users except admins.

Here is code to describe concept:

class AbstractObject {
    public $owner;
    function add($class){
        $c=new $class;
        $c->owner=$this;
        return $c;
    }
}

class Form extends AbstractObject {
    function dosomething(){
        return $this;
    }
}
class OtherForm extends Form {}

$object->add('Form')->dosomething()->owner
       ->add('OtherForm');  // etc

I think it's awesome and very practical approach.

p.s. I have to note new syntax for exceptions:

throw $this->exception('Something went bad');

using $this links exception to the object, which is at fault, which also can set default class for exception.

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.