Tag Archives: PHP

Form

Way of collecting data online from user accessing a web site.

Form data automatically available to php scripts. Dots and spaces in variable names converted to underscores.

Form data can be made into an array using the following syntax <input name=”FormArray[]”> .

Group elements by assigning the same array name to different elements; can specify keys.

$_POST superglobal contains all POST data; paired with post method

$_GET superglobal contains all GET data and $_REQUEST is independent of data source, and merges information from sources like GET, POST, and COOKIES; usage is not recommended

Sessions

Way of preserving data across a series of web site accesses by the user . session support is enabled by default . configuration options set in php.ini .SID(STRING) is a pre-defined constant within this extension.

User assigned a unique identifier, the “SESSION ID”. Session id is stored in a cookie on the client or in the url .

Site access by user triggers session id check automatically session.auto_start = 1 or upon request … using session_start().

$_SESSION is available as a global variable.

Enable session.use_only_cookies to enforce cookie usage (and prevent session ids in the url) and enable session.cookie_httponly to prevent javascript cookie access (and help prevent session hijacking via xss) .

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";
 }
?>

__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);
?>

Prepared Statements MySQL

Similar in concept to templates – contain compiled code used to run common sql operations

o Advantages:

 Query only parsed once, but allows for multiple executions, with same or different parameters (performance consideration)

 Related parameters do not need to be quoted (security consideration)

o Only feature pdo will emulate for adapters that do not support prepared statements

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;
}
}

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');

?>

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.