Php class static include

How to create static classes in PHP ?

A class is a user-defined data type that holds its own data members and member functions that can be accessed and used by creating one or more instances of that class. Every time a class is instantiated, the values that it holds are different and unique to the particular instance or object and not to that class. Static Classes brings forth a property wherein a class itself holds values that remain the same and aren’t unique. Another property of Static Classes is that we can do the aforementioned without having to create an instance of the class.

How to create a static class?
It’s fairly simple. The variables and methods that are declared and defined within a class are to be declared as static with the use of static keyword, so that they can be used without instantiating the class first.
An important point to note here is that, since this means a class variable can be accessed without a specific instance, it also means that there will only be one version of this variable. Another consequence is that a static method cannot access non-static variables and methods since these require an instance of the class.

To access the static class and it’s method use the following syntax:

Example 1: The following code returns the current date without instantiating the class Date. In this case, the format of the date and not the actual date remains the same.

Источник

Php class static include

This page describes the use of the static keyword to define static methods and properties. static can also be used to define static variables, define static anonymous functions and for late static bindings. Please refer to those pages for information on those meanings of static .

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. These can also be accessed statically within an instantiated class object.

Static methods

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside methods declared as static.

Calling non-static methods statically throws an Error .

Prior to PHP 8.0.0, calling non-static methods statically were deprecated, and generated an E_DEPRECATED warning.

Example #1 Static method example

Foo :: aStaticMethod ();
$classname = ‘Foo’ ;
$classname :: aStaticMethod ();
?>

Static properties

Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ).

It’s possible to reference the class using a variable. The variable’s value cannot be a keyword (e.g. self , parent and static ).

Example #2 Static property example

class Foo
public static $my_static = ‘foo’ ;

public function staticValue () return self :: $my_static ;
>
>

class Bar extends Foo
public function fooStatic () return parent :: $my_static ;
>
>

$foo = new Foo ();
print $foo -> staticValue () . «\n» ;
print $foo -> my_static . «\n» ; // Undefined «Property» my_static

Читайте также:  Создаем wp config php

print $foo :: $my_static . «\n» ;
$classname = ‘Foo’ ;
print $classname :: $my_static . «\n» ;

print Bar :: $my_static . «\n» ;
$bar = new Bar ();
print $bar -> fooStatic () . «\n» ;
?>

Output of the above example in PHP 8 is similar to:

foo foo Notice: Accessing static property Foo::$my_static as non static in /in/V0Rvv on line 23 Warning: Undefined property: Foo::$my_static in /in/V0Rvv on line 23 foo foo foo foo

User Contributed Notes 28 notes

Note that you should read «Variables/Variable scope» if you are looking for static keyword use for declaring static variables inside functions (or methods). I myself had this gap in my PHP knowledge until recently and had to google to find this out. I think this page should have a «See also» link to static function variables.
http://www.php.net/manual/en/language.variables.scope.php

Here statically accessed property prefer property of the class for which it is called. Where as self keyword enforces use of current class only. Refer the below example:

static protected $test = «class a» ;

public function static_test ()

echo static:: $test ; // Results class b
echo self :: $test ; // Results class a

static protected $test = «class b» ;

$obj = new b ();
$obj -> static_test ();
?>

It is worth mentioning that there is only one value for each static variable that is the same for all instances

You misunderstand the meaning of inheritance : there is no duplication of members when you inherit from a base class. Members are shared through inheritance, and can be accessed by derived classes according to visibility (public, protected, private).

The difference between static and non static members is only that a non static member is tied to an instance of a class although a static member is tied to the class, and not to a particular instance.
That is, a static member is shared by all instances of a class although a non static member exists for each instance of class.

Thus, in your example, the static property has the correct value, according to principles of object oriented conception.
class Base
public $a;
public static $b;
>

class Derived extends Base
public function __construct()
$this->a = 0;
parent::$b = 0;
>
public function f()
$this->a++;
parent::$b++;
>
>

$i1 = new Derived;
$i2 = new Derived;

To check if a method declared in a class is static or not, you can us following code. PHP5 has a Reflection Class, which is very helpful.

try $method = new ReflectionMethod( ‘className::methodName );
if ( $method->isStatic() )
// Method is static.
>
>
catch ( ReflectionException $e )
// method does not exist
echo $e->getMessage();
>

class Foo public static $bar = ‘a static property’;
>

I used instantiation to access the access the a static property directly.

A Simple ticky art, you may apply (using object to access static property in a class) with the scope resolution operator
class Shopinson const MY_CONSTANT = ‘the value of MY_CONSTANT ‘ ;
>

class Godwin extends Shopinson
public static $myconstant = ‘ The Paamayim Nekudotayim or double-colon.’ ;
public function SaySomething () echo parent :: MY_CONSTANT . PHP_EOL ; // outputs: the value of MY_CONSTANT
echo self :: $myconstant ; // outputs: The Paamayim Nekudotayim or double-colon.
>
>

Читайте также:  The java mouse deer

$my_class = new Godwin ();
print $my_class :: $myconstant ;
$my_class :: SaySomething ();
echo Godwin :: $myconstant ;
Godwin :: SaySomething ();

It should be noted that in ‘Example #2’, you can also call a variably defined static method as follows:

class Foo public static function aStaticMethod () // .
>
>

$classname = ‘Foo’ ;
$methodname = ‘aStaticMethod’ ;
$classname ::< $methodname >(); // As of PHP 5.3.0 I believe
?>

The static keyword can still be used (in a non-oop way) inside a function. So if you need a value stored with your class, but it is very function specific, you can use this:

class aclass public static function b() static $d=12; // Set to 12 on first function call only
$d+=12;
return «$d\n»;
>
>

echo aclass::b(); //24
echo aclass::b(); //36
echo aclass::b(); //48
echo aclass::$d; //fatal error

It is important to understand the behavior of static properties in the context of class inheritance:

— Static properties defined in both parent and child classes will hold DISTINCT values for each class. Proper use of self:: vs. static:: are crucial inside of child methods to reference the intended static property.

— Static properties defined ONLY in the parent class will share a COMMON value.

class staticparent static $parent_only ;
static $both_distinct ;

function __construct () static:: $parent_only = ‘fromparent’ ;
static:: $both_distinct = ‘fromparent’ ;
>
>

class staticchild extends staticparent static $child_only ;
static $both_distinct ;

function __construct () static:: $parent_only = ‘fromchild’ ;
static:: $both_distinct = ‘fromchild’ ;
static:: $child_only = ‘fromchild’ ;
>
>

$a = new staticparent ;
$a = new staticchild ;

echo ‘Parent: parent_only=’ , staticparent :: $parent_only , ‘, both_distinct=’ , staticparent :: $both_distinct , «
\r\n» ;
echo ‘Child: parent_only=’ , staticchild :: $parent_only , ‘, both_distinct=’ , staticchild :: $both_distinct , ‘, child_only=’ , staticchild :: $child_only , «
\r\n» ;
?>

will output:
Parent: parent_only=fromchild, both_distinct=fromparent
Child: parent_only=fromchild, both_distinct=fromchild, child_only=fromchild

Static variables are shared between sub classes

protected static $variable ;
>

class Child1 extends MyParent

class Child2 extends MyParent

$c1 = new Child1 ();
$c1 -> set ();
$c2 = new Child2 ();
$c2 -> show (); // prints 2
?>

To check if a function was called statically or not, you’ll need to do:

(I’ll add this to the manual soon).

class a < use t ; >
class b extends a <>

echo (new a )-> testMe ();
echo (new b )-> testMe ();

outputs
static: a // self:t
static: b // self:t

In real world, we can say will use static method when we dont want to create object instance.

validateEmail($email) if(T) return true;
return false;
>

//This makes not much sense
$obj = new Validate();
$result = $obj->validateEmail($email);

//This makes more sense
$result = Validate::validateEmail($email);

Starting with php 5.3 you can get use of new features of static keyword. Here’s an example of abstract singleton class:

protected static $_instance = NULL ;

/**
* Prevent direct object creation
*/
final private function __construct ()

/**
* Prevent object cloning
*/
final private function __clone ()

/**
* Returns new or existing Singleton instance
* @return Singleton
*/
final public static function getInstance () if( null !== static:: $_instance ) return static:: $_instance ;
>
static:: $_instance = new static();
return static:: $_instance ;
>

On PHP 5.2.x or previous you might run into problems initializing static variables in subclasses due to the lack of late static binding:

Читайте также:  Языки программирования php типы данных

class A protected static $a ;

public static function init ( $value ) < self :: $a = $value ; >
public static function getA () < return self :: $a ; >
>

class B extends A protected static $a ; // redefine $a for own use

// inherit the init() method
public static function getA () < return self :: $a ; >
>

B :: init ( ‘lala’ );
echo ‘A::$a = ‘ . A :: getA (). ‘; B::$a = ‘ . B :: getA ();
?>

This will output:
A::$a = lala; B::$a =

If the init() method looks the same for (almost) all subclasses there should be no need to implement init() in every subclass and by that producing redundant code.

Solution 1:
Turn everything into non-static. BUT: This would produce redundant data on every object of the class.

Solution 2:
Turn static $a on class A into an array, use classnames of subclasses as indeces. By doing so you also don’t have to redefine $a for the subclasses and the superclass’ $a can be private.

Short example on a DataRecord class without error checking:

abstract class DataRecord private static $db ; // MySQLi-Connection, same for all subclasses
private static $table = array(); // Array of tables for subclasses

public static function init ( $classname , $table , $db = false ) if (!( $db === false )) self :: $db = $db ;
self :: $table [ $classname ] = $table ;
>

public static function getDB () < return self :: $db ; >
public static function getTable ( $classname ) < return self :: $table [ $classname ]; >
>

class UserDataRecord extends DataRecord public static function fetchFromDB () $result = parent :: getDB ()-> query ( ‘select * from ‘ . parent :: getTable ( ‘UserDataRecord’ ). ‘;’ );

// and so on .
return $result ; // An array of UserDataRecord objects
>
>

$db = new MySQLi (. );
UserDataRecord :: init ( ‘UserDataRecord’ , ‘users’ , $db );
$users = UserDataRecord :: fetchFromDB ();
?>

I hope this helps some people who need to operate on PHP 5.2.x servers for some reason. Late static binding, of course, makes this workaround obsolete.

class foo private static $getInitial ;

public static function getInitial () if ( self :: $getInitial == null )
self :: $getInitial = new foo ();
return self :: $getInitial ;
>
>

/*
this is the example to use new class with static method..
i hope it help
*/

Hi, here’s my simple Singleton example, i think it can be useful for someone. You can use this pattern to connect to the database for example.

class MySingleton
private static $instance = null ;

private function __construct ()
$this -> name = ‘Freddy’ ;

public static function getInstance ()
if( self :: $instance == null )
print «Object created!
» ;
self :: $instance = new self ;

public function sayHello ()
print «Hello my name is < $this ->name > !
» ;

public function setName ( $name )
$this -> name = $name ;

$objA = MySingleton :: getInstance (); // Object created!

$objA -> sayHello (); // Hello my name is Freddy!

$objA -> sayHello (); // Hello my name is Alex!

$objB = MySingleton :: getInstance ();

$objB -> sayHello (); // Hello my name is Alex!

$objA -> sayHello (); // Hello my name is Bob!

Источник

Оцените статью