Steve Peterson
Steve Peterson

Reputation: 132

Having trouble detecting 'return false'

I have a PHP class that can return FALSE.

For some weird reason, I am unable to detect the FALSE return. I can detect any other return value without any trouble.

Dumbed down version....

error_reporting(E_ALL);
ini_set("display_errors", 1);

$run = new myClass();//class returns false
$response = $run->myMethod();

I've tested with the following...

!$response
!isset($response)
empty($response)
$response == false
$response === false

I'm unable to get any type of reaction for any of the conditions I'm familiar with.

If I set the return to 1 or ANYTHING other than false, $response will contain whatever the class returns. It's not like this is my first day. (it's my 2nd).

I use return false; frequently and have never had any trouble until today.

Certainly I can fix this by returning something like 'wtf' and test for that condition, but I'd really like to figure out what I'm missing here.

Thanks for any ideas.

Upvotes: 0

Views: 61

Answers (3)

Joshua Bixler
Joshua Bixler

Reputation: 531

When you make a instance of your class it will always your return a object not the return of the construct.

<?php
class SomeClass
{
    // construct function that will always return false
    public function __construct()
    {
        return false;
    }
}

class TestClass extends PHPUnit_Framework_TestCase
{
    public function testReturn()
    {
         // check if this code returns false
         $this->assertFalse(new SomeClass);
    }

    public function testAssert()
    {
        // Checks if the object return is actually of SomeClass
        $this->assertInstanceOf('SomeClass', new SomeClass);
    }

    public function testReturnOfConstruct()
    {
        //checks the return of the construct
        $object = new SomeClass;
        $this->assertFalse($object->__construct());
    }
}

Return of phpunit

phpunit --debug test3.php
PHPUnit 3.7.28 by Sebastian Bergmann.


Starting test 'TestClass::testReturn'.
F
Starting test 'TestClass::testAssert'.
.
Starting test 'TestClass::testReturnOfConstruct'.
.

Time: 7 ms, Memory: 5.25Mb

There was 1 failure:

1) TestClass::testReturn
Failed asserting that SomeClass Object () is false.

/var/www/test3.php:14

FAILURES!
Tests: 3, Assertions: 3, Failures: 1.

Upvotes: 2

Benjamin Nolan
Benjamin Nolan

Reputation: 1215

Are you trying to return false from within public function __construct() or public function MyClass()*? This won't work as the new MyClass() call will either trigger a PHP error, or return an instance of MyClass, which is never going to evaluate to false because it is an instance of an object and thus not false.

If your class does not need to be instantiated to function, you can create a static method to perform the logic you are trying to run like so:

<?php

class MyClass
{
    const COLOUR_BLUE = 'blue';

    public static function isSkyBlue()
    {
        if (isset($_POST['sky']) && $_POST['sky'] == self::COLOUR_BLUE) {
            return true;
        } else {
            return false;
        }
    }
}

// Call doThings() statically to get its value
$response = MyClass::isSkyBlue();
if ($response === false) {
    // Your logic goes here
}

Alternatively, if you are passing things to the constructor before it performs its logic, you can do the following:

<?php

class MyClass
{
    const COLOUR_BLUE = 'blue';

    protected $otherObject = null;

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

    public function isSkyBlue()
    {
        if ($this->otherObject->sky == self::COLOUR_BLUE) {
            return true;
        } else {
            return false;
        }
    }
}

// Call doThings() statically to get its value
$response = new MyClass($otherObject)
if ($response->isSkyBlue() === false) {
    // Your logic goes here
}

* Note that your constructor should always be called __construct() as using the class name for the constructor has been deprecated since PHP 5.

Upvotes: 1

user3319461
user3319461

Reputation:

If you are using a constructor, it wont return anything...it is not a normal function

Upvotes: 0

Related Questions