Found 33676 Articles for Programming

PHP WeakReference class

Malhar Lathkar
Updated on 21-Sep-2020 12:06:26

468 Views

IntroductionWith Weak references, it is possible to retain a reference to an object which does not prevent the object from being destroyed. Implementing cache like structures can be done by Weak reference.A weak reference is similar to a normal reference, except that it doesn’t prevent the garbage collector from collecting the object. If strong references to that object are not found, it will be immediately removed frommemory. This way it is possible to implement most of the benefits of a cache, with no memory issues.WeakReference class has been introduced in PHP 7.4. Before this version, same effect used to be ... Read More

PHP Traversable interface

Malhar Lathkar
Updated on 21-Sep-2020 12:04:55

470 Views

IntroductionTraversable is an abstract interface, hence it can not be directly implemented by any class. Generally, Iterator or IteratorAggregate interfaces, which extend Traversable, are used to check if implementing class is traversable using foreach construct in PHP.Certain built-in classes that implement this interface can be used in foreach and need not implement iterator interfaces. Since Traversable is an abstract interface, it doesn't have any methods in it.SyntaxTraversable {    // }When implementing IteratorAggregate or Iterator interface which extends Traversable, they must be listed before its name in the implements clause.

PHP Throwable interface

Malhar Lathkar
Updated on 21-Sep-2020 12:02:37

524 Views

IntroductionIn PHP 7, Throwable interface acts as base for any object that can be a parameter to throw statement, including Error and Exception. Both Error and Exception classes, from which predefined and user defined error and exception classes are derived respectively, implement Throwable interface. Following abstract methods are defined in Throwable interface −SyntaxThrowable {    /* Methods */    abstract public getMessage ( void ) : string    abstract public getCode ( void ) : int    abstract public getFile ( void ) : string    abstract public getLine ( void ) : int    abstract public getTrace ( void ... Read More

PHP Serializable interface

Malhar Lathkar
Updated on 21-Sep-2020 11:51:42

619 Views

IntroductionThe Serializable interface is present in PHP library to build a class that provides custimised serialzing. PHP's serialize() function is able to serialize most of the values to a storable representation. However, objects of user defined classes can not be serialized. This interface makes it possible.SyntaxSerializable {    /* Methods */    abstract public serialize ( void ) : string    abstract public unserialize ( string $serialized ) : void }MethodsSerializable::serialize — String representation of objectSerializable::unserialize — Constructs the object from serialized string representationThe built-in serialze() function Generates a storable representation of a valueserialize ( mixed $value ) : stringunserialize() ... Read More

PHP IteratorAggregate interface

Malhar Lathkar
Updated on 21-Sep-2020 11:49:43

494 Views

IntroductionIteratorAggregate interface extends abstract Traversable interface. It is implemented by a class to create external iterator. This interface introduces on abstract method called getIterator.SyntaxIteratorAggregate extends Traversable {    /* Methods */    abstract public getIterator ( void ) : Traversable }MethodsIteratorAggregate::getIterator — Retrieve an external iteratorThis function has no parameters and returns an instance of an object implementing Iterator or Traversable.IteratorAggregate ExampleIn following PHP script, a class that implements IteratorAggregate interface contains an array as a propertyThe getIterator() method returns ArrayIterator object out of this array. We can traverse the array using foreach loop.Example Live DemoOutputtraversal of array property shows following result0=>10 1=>20 2=>30 3=>40

PHP Iterable interface

Malhar Lathkar
Updated on 21-Sep-2020 11:48:02

980 Views

IntroductionIterator interface extends abstract Traversable interface. PHP provides many built-in iterators (called SPL iterators) for many routine functionalities. Examples are ArrayIterator, DirectoryIterator etc. A user class that implements Iterator interface should implement abstract methods as defined in it.SyntaxIterator extends Traversable {    /* Methods */    abstract public current ( void ) : mixed    abstract public key ( void ) : scalar    abstract public next ( void ) : void    abstract public rewind ( void ) : void    abstract public valid ( void ) : bool }MethodsIterator::current — Return the current elementIterator::key — Return the key of ... Read More

PHP Generator class

Malhar Lathkar
Updated on 21-Sep-2020 11:45:54

769 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

PHP Closure class

Malhar Lathkar
Updated on 21-Sep-2020 11:43:49

1K+ Views

IntroductionAnonymous functions (also called lambda) return object of Closure class. This class has some additional methods that provide further control over anonymous functions.SyntaxClosure {    /* Methods */    private __construct ( void )    public static bind ( Closure $closure , object $newthis [, mixed $newscope = "static" ] ) : Closure    public bindTo ( object $newthis [, mixed $newscope = "static" ] ) : Closure    public call ( object $newthis [, mixed $... ] ) : mixed    public static fromCallable ( callable $callable ) : Closure }Methodsprivate Closure::__construct ( void ) — This method exists ... Read More

PHP ArrayAcccess interface

Malhar Lathkar
Updated on 21-Sep-2020 11:42:01

263 Views

IntroductionIn PHP, ArrayAccess interface is used to develop a class that provides array like access to one of properties which is an array. Such array property may be manipulated without exposing it during object creation. ArrayAccess interface defines following abstract methodsSyntaxArrayAccess {    /* Methods */    abstract public offsetExists ( mixed $offset ) : bool    abstract public offsetGet ( mixed $offset ) : mixed    abstract public offsetSet ( mixed $offset , mixed $value ) : void    abstract public offsetUnset ( mixed $offset ) : void }MethodsArrayAccess::offsetExists − Whether an offset existsArrayAccess::offsetGet − Offset to retrieveArrayAccess::offsetSet − Assign ... Read More

PHP ArgumentCountError

Malhar Lathkar
Updated on 21-Sep-2020 11:37:53

538 Views

IntroductionPHP parser throws ArgumentCountError when arguments passed to a user defined function or method are less than those in its definition. ArgumentCountError class is inherited from TypeError classArgumentCountError ExampleIn Following example, a user defined function add() is defined to receive two arguments. However, if less than required number of arguments is provided while calling, ArgumentCountError will be thrown which can be handled with catch block.Example Live DemoOutputThis will produce following result −Too few arguments to function add(), 1 passed in C:\xampp\php\test.php on line 6 and exactly 2 expectedIn Following example, setdata() method in myclass is defined to have two formal arguments. ... Read More

Advertisements