The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
New static: The static is a keyword in PHP. Static in PHP 5.3's late static bindings, refers to whatever class in the hierarchy you called the method on. The most common usage of static is for defining static methods.
When you use static, this is to call a function without an instance of the class. The main reason is often to represent a service class which should not be instantiated many times. A static class (with only static functions) prevent you from using many OOP features like inheritance, interface implementation.
self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.
You can have static classes in PHP but they don't call the constructor automatically (if you try and call self::__construct()
you'll get an error).
Therefore you'd have to create an initialize()
function and call it in each method:
<?php
class Hello
{
private static $greeting = 'Hello';
private static $initialized = false;
private static function initialize()
{
if (self::$initialized)
return;
self::$greeting .= ' There!';
self::$initialized = true;
}
public static function greet()
{
self::initialize();
echo self::$greeting;
}
}
Hello::greet(); // Hello There!
?>
In addition to Greg's answer, I would recommend to set the constructor private so that it is impossible to instantiate the class.
So in my humble opinion this is a more complete example based on Greg's one:
<?php
class Hello
{
/**
* Construct won't be called inside this class and is uncallable from
* the outside. This prevents instantiating this class.
* This is by purpose, because we want a static class.
*/
private function __construct() {}
private static $greeting = 'Hello';
private static $initialized = false;
private static function initialize()
{
if (self::$initialized)
return;
self::$greeting .= ' There!';
self::$initialized = true;
}
public static function greet()
{
self::initialize();
echo self::$greeting;
}
}
Hello::greet(); // Hello There!
?>
you can have those "static"-like classes. but i suppose, that something really important is missing: in php you don't have an app-cycle, so you won't get a real static (or singleton) in your whole application...
see Singleton in PHP
final Class B{
static $staticVar;
static function getA(){
self::$staticVar = New A;
}
}
the stucture of b is calld a singeton handler you can also do it in a
Class a{
static $instance;
static function getA(...){
if(!isset(self::$staticVar)){
self::$staticVar = New A(...);
}
return self::$staticVar;
}
}
this is the singleton use
$a = a::getA(...);
I generally prefer to write regular non static classes and use a factory class to instantiate single ( sudo static ) instances of the object.
This way constructor and destructor work as per normal, and I can create additional non static instances if I wish ( for example a second DB connection )
I use this all the time and is especially useful for creating custom DB store session handlers, as when the page terminates the destructor will push the session to the database.
Another advantage is you can ignore the order you call things as everything will be setup on demand.
class Factory {
static function &getDB ($construct_params = null)
{
static $instance;
if( ! is_object($instance) )
{
include_once("clsDB.php");
$instance = new clsDB($construct_params); // constructor will be called
}
return $instance;
}
}
The DB class...
class clsDB {
$regular_public_variables = "whatever";
function __construct($construct_params) {...}
function __destruct() {...}
function getvar() { return $this->regular_public_variables; }
}
Anywhere you want to use it just call...
$static_instance = &Factory::getDB($somekickoff);
Then just treat all methods as non static ( because they are )
echo $static_instance->getvar();
object cannot be defined staticly but this works
final Class B{
static $var;
static function init(){
self::$var = new A();
}
B::init();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With