views:

1377

answers:

6

Hey there!

I was wondering why my php program is not returning the correct TRUE FALSE value when the class is included elsewhere

it goes something like this

source: signup.php

class signup
{
    function buildProfile()
    {
        if($logic){
            $this->success = TRUE;
        }else{
           $this->success = FALSE;
        }
    }
    function __construct()
    {
        $this->success = NULL;
        $this->buildProfile
        return $this->success;
    }
}

and elsewhere I do

include('signup.php');

$signup = new signup();

if($signup){
    successFunction();
}else....

but it's not getting $signup == true, it's getting FALSE every time

+16  A: 

Constructors always return a new instance of the class. You cannot return any other type of value from a constructor. A better way to structure your code would be something like:

source: signup.php

class Signup
{
    public $success;
    protected function buildProfile()
    {
        if($logic){
            $this->success = true;
        }else{
            $this->success = false;
        }
    }
    public function __construct()
    {
        $this->success = null;
        $this->buildProfile();
    }
}

Then when you construct the object you could do:

include('signup.php');

$signup = new Signup();

if($signup->success){
    successFunction();
}else....
VoteyDisciple
About the "if you are using PHP4" comment : no-one should use PHP 4... It's not even maintained anymore, not even for security-related bugs. If learning OOP in PHP, one has to go with PHP 5 ! (and considering the use of __construct, it is PHP 5, after all : the "public/var" thing is not the only thing that changed)
Pascal MARTIN
I agree, though alas too many people are still stuck on PHP 4. You're quite right that __construct should have been a givewaway. I'll edit accordingly.
VoteyDisciple
Since success is specified in the class (as it should be), you don't have to initialize it to NULL, since that is the default (non)value.
Dennis Haarbrink
+3  A: 

With that here :

$signup = new signup();

You are creating an instance of the class, via its constructor.

A constructor is not a normal function : I don't think it can actually "return" anything : it is just here to initialize some data in the object upon its instanciation.

With that, you $signup variable is an object ; an instance of class signup ; it's not any other kind of value, whatever return instruction you'll write in your constructor.

Quoting from wikipedia's Constructor page :

A constructor is similar to an instance method, but it differs from a method in that it never has an explicit return type, it is not inherited, and usually has different rules for scope modifiers.

Pascal MARTIN
A: 

You cannot return a value from a constructor.

A constructor's sole purpose is to initialize an object. In your example, $signup is your object, it's not a "return value" from the constructor.

Bruno Reis
A: 

You are attempting to imitate a "default functor", a feature that is not present in PHP until 5.3, e.g. you are trying to evaluate an object as if it were a function (as said above, ``new'' always returns you object instances). Objects aren't functions, so a default functor is an instance method that gets automagically executed when its object is invoked as a function.

Either way, the correct solution is in the top answer.

A: 

Perhaps a static method would be more appropriate?

http://php.net/manual/en/language.oop5.static.php

include('signup.php');

if(Signup::buildProfile()){
    successFunction();
}else....
h0tw1r3
+1  A: 

By the way, don't write things like this:

if($logic) {
    $this->success = true;
}else{
    $this->success = false;
}

write things like this:

$this->success = $logic;
Zehntor
That would depend on what the type of $logic is. If it's a string and $success is desired to be boolean, then the assigned value would be incorrect. (Although the code as given in the question would generate an "undefined" message for $logic.)
GZipp