FOLLOW ALONG:
http://slides.com/onema/the-state-of-php/live
CODE ALONG:
http://3V4L.org/
"Standing on the shoulder of giants"
class User{}
$user = new User();
<?php
namespace GeekGirl; class User
{
// GeekGirl User Code
}
<?php
$user = new GeekGirl\User();
namespace GeekGirl;
$gg_user = new User(); // User Class in the GeekGirl namespace
$user = new \User(); // User class from the Global namespace
namespace GeekGirl;
$user = new \SDConference\User();
namespace GeekGirl;
use SDConference\User;
$user = new User(); // Using User method from SDConference
namespace GeekGirl;
use SDConference\User as SdConfUser;
$user = new User(); // GeekGirl Namespace
$sd_user = new SdConfUser(); // SDConference
trait Magic {
/**
* A block of code that does a lot of work
* need by several other classes
*/
public function foo() {}
}
class Reader extends BaseReader {
use Magic;
}
class Writer {
use Magic;
}
trait Hello {
function sayHello() {
return "Hello";
}
}
trait World {
function sayWorld() {
return "World";
}
}
class MyWorld {
use Hello, World;
}
$world = new MyWorld();
echo $world->sayHello() . " " . $world->sayWorld(); //Hello World
In computer programming, an anonymous function is a function definition that is not bound to an identifier.
- Wikipedia
function doMath(callable $function, $a, $b) { return $function($a, $b); } doMath(function($a, $b) { return $a * $b; }, 9, 2); // 18
function sum ($a, $b) { return $a + $b; } $division = function($a, $b) { return $a/$b; }; function doMath(Closure $function, $a, $b) { return $function($a, $b); } doMath($division, 9, 3); // 3
A closure is a lambda function that is aware of its surrounding context
- Fabien Potencier
$division = function($a, $b) { return $a/$b; }; function doMath(Closure $function, $a, $b) { $result = $function($a, $b); return function() use ($result) {echo $result;}; } $callback = doMath($division, 9, 3); $callback(); // 3
$division = function($a, $b) { return $a/$b; }; function doMath(Closure $function, $a, $b) { $result = $function($a, $b); return function() use ($result) {echo $result;}; } $callback = doMath($division, 9, 3); $callback(); // 3 $callback = doMath(function($a, $b) { return $a * $b; }, 9, 2); // 18 $callback();
curl -s https://getcomposer.org/installer | php
curl -s https://getcomposer.org/rinstaller | php
sudo mv composer.phar /usr/local/bin/composer
{ "require": { "php": ">=5.4", "guzzlehttp/guzzle": "4.0.*" } }
<?php // test.php require_once __DIR__.'/vendor/autoload.php'; $client = new GuzzleHttp\Client(); $response = $client->get('https://google.com'); echo $response->getBody();
\{Vendor Name}\({Namespace}\)*{Class Name}
2. Each namespace separator is converted to a directory separator when loading from the file system/VendorName/Namespace/ClassName.php
class Foo_Bar_Database_Model {...}
namespace Foo\Bar\Database; class Model {...}
use Foo\Bar\Database\Model; use Custom\Database\Model as CustomModel; class MyController { $model = new Model(); $customModel = new CustomModel(); }
namespace Vendor\Package; // <== Line break use OtherVendor\OtherPackage\BazClass; // <== Line break class Foo extends Bar implements FooInterface { // <== Brace in new line public $var; // <== Visibility public function sampleFunction($a, $b = null) // <== Must not have space { // <== Brace in new line if ($a === $b) { // <== Brace in same line bar(); // <== 4 space indentation } else { BazClass::bar($arg2, $arg3); } } // <== Must be in new line }
These are amongst the least know functionality in the SPL, yet they can be very helpful.
Data structures implement the Iterator and Countable interfaces which enable them to be traversed using foreach() and counted using the count().
Some notable Data Structures are:
class FifaRankingHeap extends \SplHeap {
protected function compare($value1, $value2) {
return $value1['points'] - $value2['points'];
}}
$heap = new FifaRankingHeap();
$heap->insert(array('country' => 'Colombia', 'points' => 1137));
$heap->insert(array('country' => 'Uruguay', 'points' => 1147));
$heap->insert(array('country' => 'Argentina', 'points' => 1175));
$heap->insert(array('country' => 'Brazil', 'points' => 1242));
$heap->insert(array('country' => 'Portugal', 'points' => 1189));
$heap->insert(array('country' => 'Germany', 'points' => 1300));
$heap->insert(array('country' => 'Switzerland', 'points' => 1149));
$i = 2;
foreach ($heap as $country) {
echo $i++.$country['country'].' has '.$country['points'].' points.';
}
class Test { public function __destruct(){ echo "Destroying class ".__CLASS__; } } $array = new \SplFixedArray(6); $array[5] = new Test(); $array->setSize(5); // Destroying class Test