Category Archives: PHP

Finally

Finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

<?php
 function doSomething($a, $b) {
 return $a / $b;
 }
 
 try {
 doSomething(1);
 } catch (Exception $ex) {
 echo 1;
 } catch (ArgumentCountError $ace) {
 echo 2;
 } catch (DivisionByZeroError $dbze) {
 echo 3;
 }finally {
 print "This part is always executed\n";
 }
?>
Share

__invoke

PHP does not allow the passing of function pointers like other languages. Functions are not first class in PHP. Functions being first class mainly means that you can save a function to a variable, and pass it around and execute it at any time.

The __invoke method is a way that PHP can accommodate pseudo-first-class functions.

The __invoke method can be used to pass a class that can act as a closure or a continuation, or simply as a function that you can pass around.

The __invoke() method is called when a script tries to call an object as a function.

<?php
 class Invoke {
 public function __invoke($x)
 {
 var_dump($x);
 }
 }
 $obj = new Invoke;
 $obj(5);
?>
Share

Generator

Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to.

When a generator function is called, it returns an object that can be iterated over. When you iterate over that object (for instance, via a foreach loop), PHP will call the generator function each time it needs a value, then saves the state of the generator when the generator yields a value so that it can be resumed when the next value is required.

Once there are no more values to be yielded, then the generator function can simply exit, and the calling code continues just as if an array has run out of values.

• A mechanism to generate iterators

• A generator function returns multiple values

• Individual values are returned using the yield keyword

• Generator may use return for the final return expression; the generator´s getreturn() method gives access to this value.

function myGenerator() {
for ($i = 1; $i <= 10; $i++) {
yield $i;
}
}

Share

call_user_func_array

call_user_func_array — Call a callback with an array of parameters

Calls the callback given by the first parameter with the parameters in param_arr.

Returns the return value of the callback, or FALSE on error.

<?php
 class Test {
 public function __call($name, $args){
 call_user_func_array(array('static', "test$name"), $args);
 }
 public function testS($l) {
 echo "$l,";
 }
 }
 
 class Test2 extends Test {
 public function testS($l) {
 echo "$l,$l,";
 }
 }
 
 
 $test = new Test2();
 $test->S('A');

?>
Share

IteratorAggregate

The IteratorAggregate is an interface to create an external Iterator.

While Iterator is an interface for external iterators or objects that can be iterated themselves internally.

IteratorAggregate is an easy way to implement an Iterator. The disadvantage is you cannot add next(), key(), etc methods, as they won’t get called during a normal foreach traversal.

If you need to implement custom methods, you need to implement an OuterIterator or (easier) to extend an IteratorIterator.

Share

Pattern Modifier

The current possible PCRE modifiers are listed below. The names in parentheses refer to internal PCRE names for these modifiers. Spaces and newlines are ignored in modifiers, other characters cause error.

i (PCRE_CASELESS)
m (PCRE_MULTILINE)
s (PCRE_DOTALL)
x (PCRE_EXTENDED)
e (PREG_REPLACE_EVAL)
A (PCRE_ANCHORED)
D (PCRE_DOLLAR_ENDONLY)
S
U (PCRE_UNGREEDY)
X (PCRE_EXTRA)
J (PCRE_INFO_JCHANGED)
u (PCRE_UTF8)

Share

stream_copy_to_stream()

int stream_copy_to_stream ( resource source, resource dest [, int maxlength [, int offset]] )

Makes a copy of up to maxlength bytes of data from the current position (or from the offset position, if specified) in source to dest. If maxlength is not specified, all remaining content in source will be copied.

stream_copy_to_stream($source_file, $destination_file);

Share