1

So yeah, I was wondering if you can do it..

class Ping 
{

    function __construct()
    {
        return self::ping();
    }

    private static function ping()
    {

        if ((1 - 1) == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}

And usage is:

Class ServerHandler
{

    public function check()
    {
        if (new Ping())
        {
            ECHO 1;
        }
        else
        {
            echo 2;
        }
    }

}

So basically, it doesn't work. My website is echoing '1' instead of '2' cause false must return '2'.

Basically, I want to check if it returns true or false.

For example, a boolean variable can do that:

if (variable) 
{
    then...
}

But why can't a class do that? I mean I am returning the boolean, using the constructor? Why doesn't it work?

2
  • php.net/manual/en/language.references.return.php covers this Commented Jul 24, 2013 at 11:24
  • var_dump(new Ping()); Will show you exactly what is returned, but the OOP correct way would be $ping = new Ping(); $ping->ping(); as new Ping(); creates an instance of the ping class rather than just returning true or false. Commented Jul 24, 2013 at 11:26

3 Answers 3

5

Because the constructor is used to instantiate the class, and what you get returned is the instance itself.... though look at the new features in PHP 5.5

EDIT

I'd thought I'd read something about dereferencing (similar to array dereferencing) from an object constructor being implemented in PHP 5.5, but can't find any references to it at the moment

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

2 Comments

you mean PHP 5.4: Class member access on instantiation has been added, e.g. (new Foo)->bar() ?
That's the one @redreggae - not exactly a return from the constructor, but should be usable to achieve something along the lines of what the OP is asking - I was checking through the 5.5 changelogs, and couldn't find it
3

I don't think PHP cares about your return statement in your constructor. It should always return the object that was created (the instance of Ping). Since the instance of Ping is not null, if(new Ping()) evaluates to true and '1' is echoed.

If pinging is really just one call, why use an object? You can have this statically defined in a static library of some sort (e.g. NetUtils). If pinging is more complex and you think that it fits the object oriented model, then perform the ping in two steps.

Ping pingObj = new Ping();
pingObj->ping();

3 Comments

Fair enough. I just tried making my code more cleaner in this case without using any functions like that. But is that possible to do somehow in PHP 5.4?
I'm actually not sure. @Mark seems to know that it is a new feature in PHP 5.5. Good luck
No as-of-yet-released version of php allows you to override the return value of a constructor. You will always get the new object instance no matter what you do.
1

If you do not call construct function directly, it will always return an object.

BUT, you can do:

$ob = new Ping();
$ob->__construct();

Second line of code will return what's written in your constructor function's return. In your case it should return false.

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.