Found 1060 Articles for PHP

PHP Objects and references

Malhar Lathkar
Updated on 18-Sep-2020 11:27:25

4K+ Views

IntroductionIn PHP, objects are passed by references by default. Here, reference is an alias, which allows two different variables to write to the same value. An object variable doesn't contain the object itself as value. It only contains an object identifier which allows using which the actual object is found. When an object is sent by argument, returned or assigned, the different variables are not aliases − instead, they hold a copy of the identifier, pointing to the same object.ExamplePHP has spl_object_hash() function that returns unique hash ID of an object. In following code, two object variables, referring to same ... Read More

PHP Comparing Objects

Malhar Lathkar
Updated on 18-Sep-2020 11:25:02

3K+ Views

IntroductionPHP has a comparison operator == using which a simple comarison of two objecs variables can be performed. It returns true if both belong to same class and values of corresponding properties are are same.PHP's === operator compares two object variables and returns true if and only if they refer to same instance of same classWe use following two classes for comparison of objects with these opratorsExampletwo objects of same classExample$a=new test1(10, 20); $b=new test1(10, 20); echo "two objects of same class"; echo "using == operator : "; var_dump($a==$b); echo "using === operator : "; var_dump($a===$b);Outputtwo objects of same class using == operator ... Read More

PHP Magic Methods

Malhar Lathkar
Updated on 18-Sep-2020 11:23:16

9K+ Views

IntroductionMagic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public. These methods act as interceptors that are automatically called when certain conditions are met.Following magical methods are currently available in PHP__sleeppublic __sleep ( void ) : arrayserialize() method in class checks if it has a function name __sleep(). If so, that function is executed prior to any serialization. It ... Read More

PHP Object Interfaces

Malhar Lathkar
Updated on 18-Sep-2020 11:15:49

692 Views

IntroductionInterface is an important feature of object oriented programming by which it is possible to specify methods to be implemented by a class, without having to define how they should be implemented.PHP supports interface by way if interface keyword. Interface is similar to class but with methods without definition body. Methods in interface must be public. An inherited class that implements these methods must be defined with implements keyword instead of extends keyword, and must provide implementations of all methods in parent interface.SyntaxAll methods from interface must be defined by the implementing class, otherwise PHP parser throws exceptionExample Live DemoOutputThe error ... Read More

PHP Basics of Class and Object

Malhar Lathkar
Updated on 18-Sep-2020 09:47:28

2K+ Views

IntroductionClass is a user defined data type in PHP. In order to define a new class, PHP provides a keyword class, which is followed by a name. Any label that is valid as per PHP's naming convention (excluding PHP's reserved words) can be used as name of class. Constituents of class are defined in curly bracket that follows name of classSyntaxclass myclass{    // }Class may contain constants, variables or properties and methods - which are similar to functionsExample of classThis example shows how a Class is definedExampleFunction defined inside class is called method. Calling object's context is available inside ... Read More

PHP Autoloading Classes

Malhar Lathkar
Updated on 18-Sep-2020 09:45:17

2K+ Views

IntroductionIn order to use class defined in another PHP script, we can incorporate it with include or require statements. However, PHP's autoloading feature doesn't need such explicit inclusion. Instead, when a class is used (for declaring its object etc.) PHP parser loads it automatically, if it is registered with spl_autoload_register() function. Any number of classes can thus be registered. This way PHP parser gets a laast chance to load class/interface before emitting error.Syntaxspl_autoload_register(function ($class_name) {    include $class_name . '.php'; });The class will be loaded from its corresponding .php file when it comes in use for first timeAutoloading exampleThis example ... Read More

PHP Declaring sub-namespaces

Malhar Lathkar
Updated on 18-Sep-2020 09:35:24

291 Views

IntroductionIt is possible to create namespaces inside a namespace. Just as a directory in file system can contain sub directories in a heriarchical structure, sub-namespaces can be arranged in hierarchy. The backslash character \ is used to define the relationship between top level and sub-level namespace,In this example toplevel namespace myspace contains two sub-namespaces space1 and space2. In order to access functions/classes inside a subnamespace, first make it available by use keywordExample Live DemoOutputAbove code shows following outputHello World from space2 Hello World from space2

PHP Name Resolution Rules

Malhar Lathkar
Updated on 18-Sep-2020 09:33:15

233 Views

IntroductionIn a PHP code, appearance of namespace is resolved subject to following rules −A namespace identifier without namespace separator symbol (/) means it is referring to current namespace. This is an unqualified name.If it contains separator symbol as in myspace\space1, it resolves to a subnamespace space1 under myspace. Such type of naming is relative namespace.Name of fully qualified namespace starts with \ character. For example, \myspace or \myspace\space1.Fully qualified names resolve to absolute namespace. For example \myspace\space1 resolves to myspace\space1 namespaceIf the name occurs in the global namespace, the namespace\ prefix is removed. For example namespace\space1 resolves to space1.However, if it occurs ... Read More

PHP Defining Multiple Namespaces in same file

Malhar Lathkar
Updated on 18-Sep-2020 09:29:16

688 Views

IntroductionMore than one namespaces can be defined in a single file with .php extension. There are two diferent methods prescribed for the purpose. combination syntax and bracketed syntaxMultiple Namespaces with Combination SyntaxIn this example two namespaces are defined one below the other. Resources in first namespace are available till second definition begins. If you want to make a namespace as current load it with use keyword.Example Live DemoOutputAbove code shows following outputmyspace1 : Hello World from space1 myspace2 : Hello World from space2 Hello World from space2 Hello World from space2Multiple Namespaces with bracketed SyntaxIn following example two namespaces are defined ... Read More

PHP Aliasing/Importing namespaces

Malhar Lathkar
Updated on 18-Sep-2020 09:27:25

913 Views

IntroductionAn important feature of namespaces is the ability to refer to an external fully qualified name with an alias, or importing. PHP namespaces support following kinds of aliasing or importing −aliasing a class name, aliasing an interface name, aliasing a namespace namealiasing or importing function and constant names.In PHP, aliasing is accomplished with the use operator.use operatorExample Live Demo#test1.php OutputHello from mynamespace Hello from my new spacemultiple use statements combinedExample Live DemoOutputmyclass in mynamespace testclass in mynamespaceImporting and dynamic namessubstitute name of imported class dynamicallyExampleThe use keyword must be declared in the outermost or the global scope, or inside namespace declarations. Process ... Read More

Advertisements