PHP - constructor function doesn't return false

php 7 constructor
php constructor parameters
constructor in php tutorial point
php multiple constructors
php constructor overloading
difference between constructor and destructor in php
php constructs
call function constructor php

How can I let the $foo variable below know that foo should be false?

class foo extends fooBase{

  private
    $stuff;

  function __construct($something = false){
    if(is_int($something)) $this->stuff = &getStuff($something);
    else $this->stuff = $GLOBALS['something'];

    if(!$this->stuff) return false;
  }

}

$foo = new foo(435);  // 435 does not exist
if(!$foo) die(); // <-- doesn't work :(

You cannot return a value from the constructor. You can use exceptions for that.

function __construct($something = false){
    if(is_int($something)) $this->stuff = &getStuff($something);
    else $this->stuff = $GLOBALS['something'];

    if (!$this->stuff) {
        throw new Exception('Foo Not Found');
    }
}

And in your instantiation code:

try {
    $foo = new foo(435);
} catch (Exception $e) {
    // handle exception
}

You can also extend exceptions.

PHP OOP Constructor, Does a constructor method in PHP take the parameters declared in the class or not? I've seen on multiple sites and books and in the PHP  PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. Note: Parent constructors are not called implicitly if the child class defines a constructor.


Constructor is not supposed to return anything.

If you need to validate data before using the to create an object, you should use a factory class.

Edit: yeah , exceptions would do the trick too, but you should not have any logic inside the constructor. It becomes a pain for unit-testing.

Constructor in PHP, PHP provides you with a special method to help initialize object's properties called constructor. To add a The first constructor is empty, it does nothing. The constructor is the PHP5 OOP (Object Oriented Programming) concept. The constructor is associated with the classes we declare in the program.


You can try

<?php

function __construct($something = false){ 
    $this->stuff = $something; 
}

static function init($something = false){
    $stuff = is_int($something) ? &getStuff($something) : $GLOBALS['something'];
    return $stuff ? new self($stuff) : false;
}


$foo = foo::init(435);  // 435 does not exist
if(!$foo) die();

PHP Constructor and Destructor, PHP doesn't support function overloading hence we cannot have multiple implementations for constructor in a class. PHP Destructor. PHP Destructor method is  Constructor is not supposed to return anything. If you need to validate data before using the to create an object, you should use a factory class. Edit: yeah , exceptions would do the trick too, but you should not have any logic inside the constructor. It becomes a pain for unit-testing.


PHP Constructor and Destructor, Note: If you create a __construct() function (it is your choice,) PHP will automatically call 'Fatal error: Cannot access private property person::$​pinn_number in . PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP JSON PHP Exceptions PHP OOP PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Traits


Object Oriented PHP for Beginners: Page 3 – KillerPHP.com, Constructors: A constructor is a key concept in object oriented programming in PHP. Constructor in PHP is special type of function of a class which is automatically  Whenever a constructor is used in a PHP program, it is not mandatory to have a destructor function to complement its functionality. But it is considered as a good practice to have destructor in the program where a constructor is called for.


PHP, function __construct() { // initialize the object and its properties by assigning //​values }; __destruct(): Constructors start with two underscores and generally look like normal PHP functions. Sometimes Destructors cannot be overloaded. To create and initialize a class object in a single step, PHP provides a special method called as Constructor, which is used to construct the object by assigning the required property values while creating the object. And for destroying the object Destructor method is used. Syntax for defining Constructor and Destructor