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
-
Economics & Finance
Articles by Malhar Lathkar
Page 2 of 11
PHP WeakReference class
PHP's WeakReference class allows you to retain a reference to an object without preventing the garbage collector from destroying it. This is useful for implementing cache-like structures without memory issues, as the object can be freed when no strong references remain. What are Weak References? A weak reference differs from a normal reference because it doesn't prevent garbage collection. When all strong references to an object are removed, the object is immediately destroyed, even if weak references still exist. This enables efficient caching without memory leaks. Syntax WeakReference { /* Methods */ ...
Read MorePHP Traversable interface
The Traversable interface is PHP's base interface for making objects work with foreach loops. It's an abstract interface that cannot be implemented directly − instead, you implement either Iterator or IteratorAggregate which extend Traversable. Syntax Traversable { // No methods - this is an abstract interface } Using IteratorAggregate The simplest way to make a class traversable is by implementing IteratorAggregate ? Apple Banana Cherry Using Iterator Interface For more control, implement the Iterator interface directly ? ...
Read MorePHP Throwable interface
In PHP 7, the Throwable interface serves as the base interface for any object that can be thrown using the throw statement, including Error and Exception. Both Error and Exception classes implement this interface, making it possible to catch all throwable objects uniformly. Syntax Throwable { /* Methods */ abstract public getMessage ( void ) : string abstract public getCode ( void ) : int abstract public getFile ( void ) : string abstract public getLine ( ...
Read MorePHP Serializable interface
The Serializable interface in PHP allows custom classes to define their own serialization behavior. While PHP's built-in serialize() function can handle most data types, objects of user-defined classes need this interface to control how they are converted to and from string representations. Syntax Serializable { /* Methods */ abstract public serialize() : string abstract public unserialize(string $serialized) : void } Methods Serializable::serialize() − Returns a string representation of the object Serializable::unserialize() − Reconstructs the object from its serialized string representation ...
Read MorePHP Iterable interface
The Iterator interface extends the abstract Traversable interface in PHP. It allows user-defined classes to become iterable using foreach loops. PHP provides many built-in iterators (called SPL iterators) like ArrayIterator, DirectoryIterator, etc. A user class implementing the Iterator interface must implement all its abstract methods. Syntax Iterator extends Traversable { /* Methods */ abstract public current ( void ) : mixed abstract public key ( void ) : scalar abstract public next ( void ) : void abstract public rewind ( void ...
Read MorePHP ArrayAcccess interface
In PHP, the ArrayAccess interface allows objects to be accessed like arrays using square bracket notation. When a class implements this interface, you can use $obj['key'] syntax to interact with the object's internal data without exposing the underlying array property directly. Syntax ArrayAccess { /* Methods */ abstract public offsetExists ( mixed $offset ) : bool abstract public offsetGet ( mixed $offset ) : mixed abstract public offsetSet ( mixed $offset , mixed $value ) : void ...
Read MorePHP ArgumentCountError
PHP throws ArgumentCountError when a function or method is called with fewer arguments than required in its definition. The ArgumentCountError class inherits from TypeError and can be caught using try-catch blocks. Basic Function Example Here's an example where a user-defined function add() expects two arguments but receives only one ? Too few arguments to function add(), 1 passed and exactly 2 expected Class Method Example ArgumentCountError also occurs with class methods when insufficient arguments are provided ? Too few arguments to function MyClass::setData(), ...
Read MorePHP ArithmeticError
The ArithmeticError class is inherited from the Error class. This type of error occurs while performing certain mathematical operations. One such scenario is attempting to perform a bitwise shift operation by a negative amount. This error is also thrown when a call to the intdiv() function results in a value that is beyond the legitimate boundaries of an integer. Bitwise Shift with Negative Number In the following example, an attempt is made to use the binary shift operator with a negative operand. This results in an ArithmeticError ? Bit shift by negative ...
Read MorePHP AssertionError
The AssertionError class is a subclass of the Error class in PHP. This exception is thrown when the assert() function evaluates to FALSE. The assert() function checks if a given assertion is true and throws an AssertionError if the condition fails. Syntax // PHP 5 and PHP 7 assert ( mixed $assertion [, string $description ] ) : bool // PHP 7 only assert ( mixed $assertion [, Throwable $exception ] ) : bool Parameters Parameter Description assertion String or boolean expression to evaluate description Failure message ...
Read MorePHP CompileError
In PHP 7.3 onwards, CompileError exception has been added. This class inherits the Error class. Some error conditions that previously resulted in fatal errors now throw a CompileError. This affects compilation errors that are likely to be thrown by the token_get_all() function when parsing invalid PHP syntax. The token_get_all() function uses Zend lexical scanner to parse a given string into PHP language tokens. When used in TOKEN_PARSE mode, it can throw a CompileError for invalid PHP code. Syntax token_get_all ( string $source [, int $flags = 0 ] ) : array Parameters ...
Read More