PHP Syntax OOP Classes Components Fields Scopes Static

 

- Info:

- Field is declared as static by using keyword final.

- Value of static fields is shared between class and all objects.

- Use self::$name to access static member from within a class method.

- Use Person::$name to access static member from outside the class.

 

- Example:

- This example shows how to define Class with two fields and one function.

- Creat PHP Console Application Test.php.

 

Test.php

<?php

class Person {

static $name = "John\n";

function say() { echo self::$name; } //Use self:: to access static member from method.

}

echo Person::$name;

$john = new Person();

$john->say();

?>