__toString

The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted.

Converting objects to strings

The magic method __tostring() is called, if available o

Includes print, string interpolation, operation with strings, calling functions that expect strings, …

Example

<?php
 class TestClass{
 public $foo;
 public function __construct($foo){
 $this->foo = $foo;
 }

public function __toString(){
 return $this->foo;
 }
 }
 $class = new TestClass('Hello');
 echo $class;
?>

Closures

Anonymous functions, implemented in PHP 5.3, yield objects of this type. This fact used to be considered an implementation detail, but it can now be relied upon. Starting with PHP 5.4, this class has methods that allow further control of the anonymous function after it has been created.

• Enable creation of functions without specifying a name

• Implemented using the closure class

• Commonly used as param value for callback functions, or alternatively as variable values

• To inherit variables from parent scope (function in which closure was declared), these variables must be declared in the function header with the “USE” keyword, or passing parameters in the call line

• new closure TYPE HINT

File Wrappers

o Provide information on protocols and encodings

 can be any file wrapper

 allows for two pipelines at most – for reading & writing

o Prefix in front of a file path

file:// php://
http:// compress.zlib://
https:// compress.bzip2://
ftp:// ftps://

o Custom Wrappers

stream_wrapper_register(protocol, classname)

Registers a protocol; implementation is part of the class.

 the class implements standard functionality like reading, writing, or changing the file position
 php_user_filter is a predefined class in php and is used in conjunction with user-defined filters

stream_context_create

Stream_context_create — Creates a stream context.

Creates and returns a stream context with any options supplied in options preset.

<?php
 $opts = array(
 'http'=>array(
 'method'=>"GET",
 'header'=>"Accept-language: en\r\n" .
 "Cookie: foo=bar\r\n"
 )
 );

$context = stream_context_create($opts);
 $fp = fopen('http://www.phpcodez.com', 'r', false, $context);
 fpassthru($fp);
 fclose($fp);
?>

Arrays

• Way of ordering data by associating values to keys
• Unique keys are associated with a single value, or set of values
• Arrays can be nested, so that a value in one array actually represents a complete other array (multi-dimensional arrays)

Creating Arrays

INDEXED NUMERICALLY (INDEXED ARRAY)

o EX: $x = array(‘a’, ‘b’, ‘c’);
o EX: $x = [‘a’, ‘b’, ‘c’];
o EX: $x = array(0 => ‘a’, 1 => ‘b’, 2 => ‘c’);
o EX: $x = [0 => ‘a’, 1 => ‘b’, 2 => ‘c’];

INDEXED WITH STRINGS (ASSOCIATIVE ARRAY)

$x = array(
‘XML’ => ‘eXtensible Markup Language’
);
$x = [
‘XML’ => ‘eXtensible Markup Language’
];

Filling Arrays

• range() CREATES AN ARRAY WITH VALUES FROM AN INTERVAL

o DEFAULT STEP IS “1”

o EX: $x = range(1.2, 4.1) // == array(1.2, 2.2, 3.2)