Articles on Trending Technologies

Technical articles with clear explanations and examples

PHP Name Resolution Rules

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 291 Views

In PHP, namespace resolution follows specific rules that determine how the interpreter locates classes, functions, and constants. Understanding these rules is crucial for writing maintainable code with proper namespace organization. Types of Namespace Names PHP recognizes three types of namespace identifiers − Unqualified names − Names without namespace separator (\). They refer to the current namespace. Qualified names − Names containing separator symbol like myspace\space1. These resolve to subnamespaces. Fully qualified names − Names starting with \ character like \myspace\space1. These resolve to absolute namespaces. Resolution Rules Unqualified Names Names without separators ...

Read More

PHP Defining Multiple Namespaces in same file

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 757 Views

In PHP, you can define multiple namespaces within a single file using two different syntaxes: combination syntax (sequential) and bracketed syntax (enclosed). Each method has its own use cases and rules. Multiple Namespaces with Combination Syntax In combination syntax, namespaces are defined sequentially − one after another. The first namespace remains active until the second namespace declaration begins. Example myspace1 : Hello World from space1 myspace2 : Hello World from space2 Calling with fully qualified names: Hello World from space1 Hello World from space2 Multiple Namespaces with Bracketed ...

Read More

PHP Aliasing/Importing namespaces

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 986 Views

In PHP, namespaces support aliasing and importing to simplify references to fully qualified names. You can alias class names, interface names, namespace names, and function/constant names using the use operator. Basic Aliasing with use Operator The use operator allows you to create aliases for namespaced elements − Hello from mynamespace Hello from my new space Multiple use Statements You can import multiple classes or functions in a single use statement − myclass in mynamespace testclass in mynamespace Dynamic Class Names with ...

Read More

PHP Accessing Global classes

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 414 Views

When PHP parser encounters an unqualified 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 Classes In following example, a new class uses predefined stdClass as base class. We refer it by prefixing \ to specify global class ? Raju Accessing Classes from Included Files Included files will default to the global namespace. Hence, to refer to a class from included file, it must be ...

Read More

PHP Global space

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 308 Views

In PHP, when no namespace is explicitly defined, all classes, functions, and constants are placed in the global namespace. You can access global namespace elements from within any namespace by prefixing the name with a backslash (\). Using Global Space Specification When you're inside a namespace and need to call a global function, prefix it with \ to explicitly reference the global namespace ? Global Namespace Example Files without namespace declarations default to the global namespace ?

Read More

PHP Defining namespace

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 154 Views

PHP namespaces provide a way to encapsulate and organize code by grouping related classes, functions, and constants. The namespace keyword must be declared as the very first statement in a PHP file, right after the opening Example Here's a simple example of defining a namespace with a class and function − Namespace Declaration Rules If the namespace declaration is not at the top of the file, PHP throws a fatal error. The following example demonstrates this − PHP Fatal error: Namespace declaration statement has ...

Read More

PHP Generators.

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 4K+ Views

Traversing a big collection of data using looping constructs 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 ...

Read More

PHP Generators vs Iterator objects

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 621 Views

In PHP, generators and iterator objects both provide ways to iterate over data, but they work differently. When a generator function is called, it returns a Generator object that implements the Iterator interface, making it behave like an iterator with some limitations. Iterator Interface Methods The Iterator interface defines the following abstract methods − Iterator::current — Return the current element Iterator::key — Return the key of the current element Iterator::next — Move forward to next element Iterator::rewind — Rewind the Iterator to the first element Iterator::valid — Checks if current position is valid A ...

Read More

PHP Nested Exception

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 691 Views

In PHP, nested exceptions allow you to handle multiple types of errors at different levels. You can nest try-catch blocks to any desired depth, with exceptions being handled in reverse order − innermost exceptions are processed first. Syntax The basic structure of nested exception handling ? try { // Outer try block try { // Inner try block // Code that may throw exceptions } catch (SpecificException $e) ...

Read More

PHP Interaction between finally and return

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 2K+ Views

In PHP, there is a peculiar behavior of the finally block when either the try block or catch block contains a return statement. Normally, a return statement causes program control to go back to the calling position. However, in functions with try/catch blocks containing return statements, the finally block is always executed first before returning. Example In the following example, the div() function has a try-catch-finally construct. The try block returns the result of division when no exception occurs. In case of an exception, the catch block returns an error message. However, in either case, the statement in ...

Read More
Showing 1–10 of 61,284 articles
« Prev 1 2 3 4 5 6129 Next »
Advertisements