Found 1046 Articles for PHP

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

243 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

154 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

492 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

730 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

PHP Accessing Global classes

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

263 Views

IntroductionWhen PHP parser encounters an unqulified identifier such as class or function name, it resolves to current namespace. Therefore, to access PHP's predefined classes, they must be referred to by their fully qualified name by prefixing \.Using built-in classIn following example, a new class uses predefined stdClass as base class. We refer it by prefixing \ to specify global classExampleIncluded files will default to the global namespace. Hence, to refer to a class from included file, it must be prefixed with \Example#test1.php This file is included in another PHP script and its class is referred with \when this file is included ... Read More

PHP Global space

Malhar Lathkar
Updated on 18-Sep-2020 09:23:08

189 Views

IntroductionIn absence of any namespace definition, all definitions of class, function etc. are placed in a global namespace. If a name is prefixed with \ , it will mean that the name is required from the global space even in the context of the namespace.Using global space specificationExampleIncluded files will default to the global namespace.Example#test1.php this will print empty stringwhen this file is included in another namespaceExample#test2.php OutputThis will print following outputtestspace

PHP Defining namespace

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

125 Views

IntroductionDeclaration of class, function and constants inside a namespace affects its acess, although any other PHP code can be present in it. PHP's namespace keyword is used to declare a new namespace. A file with .php extension must have namespace declaration in very first line after If namespace declaration is not at the top of file, PHP parser throws fatal errorExample Live Demo Hello world ?>OutputAbove code now returns name following errorPHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the scriptOnly declare construct can appear before namespace declarationExample

PHP namespace keyword and __NAMESPACE__ constant

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

317 Views

IntroductionIn PHP, namespace keyword is used to define a namespace. It is also used as an operator to request access to certain element in current namespace. The __NAMESPACE__ constant returns name of current namespace__NAMESPACE ConstantFrom a named namespace, __NAMESPACE__ returns its name, for global and un-named namespace it returns empty striingExample Live Demo#test1.php OutputAn empty string is returnedname of global namespace :For named namespace, its name is returnedExample Live DemoOutputname of current namespace : myspaceDynamic name construction__NAMESPACE__ is useful for constructing name dynamicallyExample Live Demo

Advertisements