- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1046 Articles for PHP
315 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.
388 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
468 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
362 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
739 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
614 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
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
140 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
415 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
176 Views
IntroductionArithmeticError class is inherited from Error class. This type of error may occurwhile performing certain mathemetical operations. One such scenario is attempt to perform bitwise shift operation by negative amount. This error is also thrown when call to intdiv() function results in value such that it is beyond legitimate boundaries of integer.ArithmeticError ExampleIn Following example, an attempt is made to use binary shift operator with negative operand. This results in ArithmeticError.Example Live DemoOutputThis will produce following result −Bit shift by negative numberIf call to intdiv() function results in invalid integer, ArithmeticError is thrown. As shown in the example below, the minimum allowed integer ... Read More