namespace keyword and __NAMESPACE__ constant

(PHP 5 >= 5.3.0, PHP 7) Examples: __NAMESPACE__ example, namespaced code PHP supports two ways of abstractly accessing elements within the current namespace, the __NAMESPACE__ magic constant, and the namespace keyword. The value of __NAMESPACE__ is a string that contains the current namespace name. In global, un-namespaced code, it contains an empty string. <?php namespace MyPro

Namespaces and dynamic language features

(PHP 5 >= 5.3.0, PHP 7) Examples: Dynamically accessing elements PHP's implementation of namespaces is influenced by its dynamic nature as a programming language. Thus, to convert code like the following example into namespaced code: example1.php: <?php class classname {     function __construct()     {         echo __METHOD__,"\n";     } } function funcname() {     echo __FUNCTIO

Using namespaces: Basics

(PHP 5 >= 5.3.0, PHP 7) Examples: file1.php Here is an example of the three kinds of syntax in actual code: <?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class foo {     static function staticmethod() {} } ?> file2.php <?php namespace Foo\Bar; include 'file1.php'; const FOO = 2; function foo() {} class foo {     static function staticmethod() 

Defining multiple namespaces in the same file

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring multiple namespaces, simple combination syntax Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. <?php namespace MyProject; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */  } namespace AnotherProject; const CONNECT_OK = 1; class Connection { /* ... */ } funct

Declaring sub-namespaces

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring a single namespace with hierarchy Much like directories and files, PHP namespaces also contain the ability to specify a hierarchy of namespace names. Thus, a namespace name can be defined with sub-levels: <?php namespace MyProject\Sub\Level; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */  } ?>

Defining namespaces

(PHP 5 >= 5.3.0, PHP 7) Examples: Declaring a single namespace Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants. Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the to

Namespaces overview

(PHP 5 >= 5.3.0, PHP 7) Examples: Namespace syntax example PHP Namespaces provide a way in which to group related classes, interfaces, functions and constants. Here is an example of namespace syntax in PHP: <?php namespace my\name; // see "Defining Namespaces" section class MyClass {} function myfunction() {} const MYCONST = 1; $a = new MyClass; $c = new \my\name\MyClass; // see

Object Serialization

Examples: serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. Using serialize to save an object will save all variables in an object. The methods in an object will not be saved, only the name of the class. In order to be able to unserialize() an object, the class of that object needs to be defined. That is, if you have an object of cla

Objects and references

Examples: References and Objects One of the key-points of PHP 5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. This section rectifies that general thought using some examples. A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows o

Late Static Bindings

Examples: self:: usage Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined: <?php class A {     public static function who() {         echo __CLASS__;     }     public static function test() {         self::who();     } } class B extends A {     public static function who() {         echo __CLASS__;     } } B::test(); ?> The above example will output: A