Autor Zpráva
Monkeys
Profil *
Ak deklarujem premennu takto:

private $a = 6;
private $b = isset($_GET['a']) ? $_GET['a'] : 1;
tak v druhom pripade to hodi chybu.
Moze mi niekto vysvetlit preco tomu tak je ?

Maju sa premenne "naplnat" inicializovat az v __construct"ore" ?

function __construct(){
$this->a = 6;
$this->b = isset($_GET['a']) ? $_GET['a'] : 1;
}

dik za odpoved
M.
juriad
Profil

Properties

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.

http://www.php.net/manual/en/language.oop5.properties.php
Alphard
Profil
První způsob nejde, druhý není vhodný. Nejlépe zřejmě vytvořit setter a explicitně nastavit. Třída by neměla přímo pracovat s globálními proměnnými (pokud to teda není jediná třída v projektu, která zapouzdřuje práci s nimi :-)).

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0