This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Tap Here!
Smartphone icons created by Freepik - Flaticon
CSS Fundamentals
HTML Fundamentals
JavaScript Fundamentals
SQL Fundamentals
PHP Fundamentals
Module 1. Intro
Module 2. Data Types
Module 3. Operators
Module 4. Arrays, Superglobals
Module 5. PHP Forms
Module 6. PHP Advanced
Module 7. PHP OOP
Module 8. MySQL Database
Module 9. PHP XML
Module 10. PHP - AJAX
7.12 PHP Static Properties
Static properties can be called directly - without creating an instance of a class.
Static properties are declared with the static keyword:
Syntax
<?php
class ClassName {
public static $staticProp = "W3Schools";
}
?>
To access a static property use the class name, double colon (::), and the property name:
Syntax
ClassName::staticProp;
Let's look at an example:
<?php
class pi {
public static $value = 3.14159;
}
// Get static property
echo pi::$value;
?>
Example Explained
Here, we declare a static property: $value. Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).
PHP - More on Static Properties
A class can have both static and non-static properties. A static property can be accessed from a method in the same class using the self keyword and double colon (::):
<?php
class pi {
public static $value=3.14159;
public function staticValue() {
return self::$value;
}
}
$pi = new pi();
echo $pi->staticValue();
?>
To call a static property from a child class, use the parent keyword inside the child class:
<?php
class pi {
public static $value=3.14159;
}
class x extends pi {
public function xStatic() {
return parent::$value;
}
}
// Get value of static property directly via child class
echo x::$value;
// or get value of static property via xStatic() method
$x = new x();
echo $x->xStatic();
?>
Navigate this module
Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM .
Smartphone icons created by Freepik - Flaticon
Module 7 quiz
Example files created in this module:
PHP Classes/Objects - Define a Class
PHP Classes/Objects - Define Objects part 1
PHP Classes/Objects - Define Objects part 2
PHP Classes/Objects - The $this Keyword part 1
PHP Classes/Objects - The $this Keyword part 2
PHP Classes/Objects - instanceof
PHP Constructor part 1
PHP Constructor part 2
PHP Destructor part 1
PHP Destructor part 2
PHP Access Modifiers part 1
PHP Access Modifiers part 2
PHP Inheritance
PHP Inheritance - Inheritance and the Protected Access Modifier part 1
PHP Inheritance - Inheritance and the Protected Access Modifier part 2
PHP Inheritance - Overriding Inherited Methods
PHP Inheritance - The final Keyword part 1
PHP Inheritance - The final Keyword part 2
PHP Class Constants part 1
PHP Class Constants part 2
PHP Abstract Classes part 1
PHP Abstract Classes part 2
PHP Abstract Classes part 3
PHP Interfaces - Using Interfaces part 1
PHP Interfaces - Using Interfaces part 2
PHP - Using Traits
PHP - Using Multiple Traits
PHP Static Methods part 1
PHP Static Methods part 2
PHP Static Methods part 3
PHP Static Methods part 4
PHP Static Properties part 1
PHP Static Properties part 2
PHP Static Properties part 3
menu Test (text file for navigating the site)
Drop Down Menu
PHP Namespaces - Declaring a Namespace
PHP Namespaces - Using Namespaces part 1
PHP Namespaces - Using Namespaces part 2
PHP Namespaces - Namespace Alias part 1
PHP Namespaces - Namespace Alias part 2
PHP Iterables - Using Iterables part 1
PHP Iterables - Using Iterables part 2
PHP Iterables - Creating Iterables