Notations
::
⋅ for static and constant properties–methods; can be called directly from within the class:
Person::isAlive
Person::greet()
->
⋅ for instance properties‒methods; can ONLY be called through an instance:
$me->isAlive
$me->greet()
class Example { # new Example("prop1", "prop2")
public $propOne;
public $propTwo;
private $propThree = 'private property!';
const propFour = 'a constant value!'; # Example::propFour
public function __construct($propOne, $propTwo) {
$this->propOne = $propOne;
$this->propTwo = $propTwo;
}
public static function statMethod(){ # Example::statMethod()
echo 'static method!';
}
public function method(){ # exInstance->method()
echo 'a method!';
$prop3 = $this->propThree; # exInstance->method()->prop3
}
}
final
before a property–method prohibits child classes to override that prop–meth:
final public myProperty = "final value!";
How a class works
class myClass {
public $var; //public vars are needed to get values for outside use - can ONLY BE USED through 1)making an INSTANCE of a class and ACCESSING with $classn->$var;
public STATIC $var2; //STATIC keyword allows to call directly from outside with classn::$var
private function privMethod ($a) {
$privvar = $a;
return $privvar;
}
function pubMethod($a) {
$pubvar = $this->privMethod($a); //get priv method value and save to var to pass on
$this->var = $pubvar; //$this keyword is needed to set value of vars (as opposed to constants )
}
}
$w = new myClass();
$w->pubMethod("uus");
$w = classn::$var; //static var