oop - When should I declare variables in a PHP class? -
i'm new oop paradigm, there's simple explanation question...
do need declare public object-wide variables in class? example:
<?php class testclass { var $declaredvar; function __construct() { $this->declaredvar = "i declared variable."; $this->undeclaredvar = "i wasn't declared, still work."; } function display() { echo $this->declaredvar . "<br />"; echo $this->undeclaredvar; echo "<br /><br />"; } } $test = new testclass; $test->display(); $test->declaredvar = "the declared variable changed."; $test->undeclaredvar = "the undeclared variable changed."; $test->display(); ?>
in code, though $declaredvar
declared variable, $undeclaredvar
accessible , useable--it seems act if had declared public.
if undeclared class variables accessible that, what's point of declaring them front?
that variable isn't uninitialized, it's undeclared.
declaring variables in class definition point of style readability. plus can set accessibility (private or public).
anyway, declaring variables explicitly has nothing oop, it's programming-language-specific. in java can't because variables must declared explicitly.
Comments
Post a Comment