Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
PHP Name Resolution Rules
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 MorePHP Defining Multiple Namespaces in same file
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 MorePHP Aliasing/Importing namespaces
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 MorePHP Accessing Global classes
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 MorePHP Global space
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 MorePHP Defining namespace
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 MorePHP Generators.
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 MorePHP Generators vs Iterator objects
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 MorePHP Nested Exception
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 MorePHP Interaction between finally and return
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