 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1716 of 3366
 
 
			
			301 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
 
 
			
			251 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
 
 
			
			701 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
 
 
			
			928 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
 
 
			
			370 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
 
 
			
			273 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
 
 
			
			148 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
 
 
			
			446 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
 
 
			
			3K+ Views
IntroductionTraversing a big collection of data using looping construct such as foreach would require large memory and considerable processing time. With generators it is possible to iterate over a set of data without these overheads. A generator function is similar to a normal function. However, instead of return statement in a function, generator uses yield keyword to be executed repeatedly so that it provides values to be iterated.The yield keyword is the heart of generator mechanism. Even though its use appears to be similar to return, it doesn't stop execution of function. It provides next value for iteration and pauses ... Read More
 
 
			
			575 Views
IntroductionWhen a generator function is called, internally, a new object of Generator class is returned. It implements the Iterator interface. The iterator interface defines following abstract methodsIterator::current — Return the current elementIterator::key — Return the key of the current elementIterator::next — Move forward to next elementIterator::rewind — Rewind the Iterator to the first elementIterator::valid — Checks if current position is validGenerator acts as a forward-only iterator object would, and provides methods that can be called to manipulate the state of the generator, including sending values to and returning values from it.Generator as interatorIn following example, generator functions yields lines in a file ... Read More