tags:

views:

39

answers:

2

starting use oop

why:

class user 
{
    private $pdo;

    function __construct()
    {
        $this->pdo = singleton::get_instance()->PDO_connection();
    }

...
}

this works fine. but this:

class user 
{
    private $pdo = singleton::get_instance()->PDO_connection();

...
}

this does not working. Error parse error, expecting ','' or ';'' in ...

what is wrong with second variant?

A: 

Class properties cannot be assigned at declaration using functions. Scalar values, constants (though not constants of the current class), and arrays only.

bob-the-destroyer
+3  A: 

See the last sentence of the first paragraph of Properties in the PHP OOP documentation:

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

In other words, the database handler returned by this statement is not a constant value and therefore will not be available at compile time:

singleton::get_instance()->PDO_connection();
karim79