Malhar Lathkar

Malhar Lathkar

103 Articles Published

Articles by Malhar Lathkar

Page 7 of 11

PHP Objects and references

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

In 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 access to the actual object. 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. Object Assignment PHP has spl_object_hash() function that returns unique hash ID of an object. In the following code, two object ...

Read More

PHP Magic Methods

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

Magic methods in PHP are special methods that are automatically called when certain conditions are met. These methods are named with double underscore (__) as prefix and must be declared public. They act as interceptors that provide dynamic behavior to your classes. Object Access Magic Methods __get() and __set() These methods handle access to inaccessible or non-existing properties − John __isset() and __unset() These methods handle isset() and unset() calls on inaccessible properties − bool(true) bool(false) String and Function Magic ...

Read More

PHP Autoloading Classes

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

In PHP, you can use classes from other files without explicitly including them by using autoloading. When PHP encounters an undefined class, it automatically attempts to load the class file if it's registered with the spl_autoload_register() function. Syntax spl_autoload_register(function ($class_name) { include $class_name . '.php'; }); The class will be loaded from its corresponding .php file when it's first used for object instantiation or any other class operation. Basic Autoloading Example Here's how to register a class for autoloading ? test1 object created ...

Read More

PHP Declaring sub-namespaces

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

PHP allows creating nested namespaces, similar to how directories contain subdirectories in a file system. Sub-namespaces are declared using the backslash character \ to define the hierarchical relationship between parent and child namespaces. Syntax To declare a sub-namespace, use the following syntax ? namespace ParentNamespace\ChildNamespace; Example Here's how to create and use sub-namespaces with functions ? Hello World from space1 Hello World from space2 Using the 'use' Keyword You can import sub-namespaces with the use keyword for cleaner code ? ...

Read More

PHP Name Resolution Rules

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 295 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 764 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 993 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 423 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 312 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 161 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
Showing 61–70 of 103 articles
« Prev 1 5 6 7 8 9 11 Next »
Advertisements