Lyubomir Filipov * Startup Factory 2016 * @FilipovG
Lyubomir Filipov
PHP Dev
Enthusiast
PHP script tags
PHP ASP tags
<script language="php">
// Code here
</script>
<%
// Code here
%>
<%=$varToEcho; %>
• ereg()
• eregi()
• ereg_replace()
• eregi_replace()
• split()
• spliti()
• sql_regcase()
Changes you will have to make:
- add delimiters ( ' / ' )
- using the 'i' modifier
- greediness change
Fatal error: Switch statements may only contain one default clause
switch ($expr) {
default:
echo "Hello World";
break;
default:
echo "Goodbye Moon!";
break;
}
Migrating to Procedural mysqli
- switch adapters
all variables are evaluated from left to right
// Syntax
$$var['key1']['key2'];
// PHP 5.x:
// Using a multidimensional array value as variable name
${$var['key1']['key2']};
// PHP 7:
// Accessing a multidimensional array within a variable-variable
($$var)['key1']['key2'];
// Syntax
$var->$prop['key'];
// PHP 5.x:
// Using an array value as a property name
$var->{$prop['key']};
// PHP 7:
// Accessing an array within a variable-property
($var->$prop)['key'];
// Access a static property
(expression)::$foo;
// Call a static method
(expression)::foo();
// Call a callable
(expression)();
// Access a character
(expression){0};
// Define and immediately call a closure without assignment
(function() { /* ... */ })();
// Call a callable within an object property
($obj->callable)();
// Call a dynamic static method
["className", "staticMethod"]();
// Call a dynamic instance method
[$object, "method"]();
// Use a string scalar as a class name
'className'::staticMethod();
//Changes to the global keyword
global $$foo->bar; // Now a parse error
// instead make sure to add braces to make it unambiguous
global ${$foo->bar};
global will now only take unambiguous variables
Null Coalesce Operator - ??
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
Constant arrays
define('FOO', [
'bar' => 'baz',
'bat' => 'qux'
]);
echo FOO['bar'];
Unpacking Objects Using list()
$object = new \ArrayObject(json_decode($json));
list($foo, $bar, $baz) = $object;
random_bytes ( int $length )
random_int ( int $min , int $max )
Filtered unserialize()
Salts Deprecated in password_hash()
try {
new MessageFormatter('en_US', null);
} catch (\IntlException $e) {
//handle error
}
internal classes will throw an exception on __construct() failure
all fatal errors and catchable fatal errors are now engine exceptions
we can now handle fatal errors in our code using try...
Fatal error: Class NoBeerException cannot implement
interface Throwable, extend Exception or Error instead
interface CustomExceptionInteface extends \Throwable {
...
}
class CustomException
extends \Exception implements CustomExceptionInteface
{
// implement interface methods
}
IntlChar::charName("\u{1F422}"); \\TURTLE
IntlChar::isAlpha($char);
IntlChar::isAlnum($char);
IntlChar::isPunct($char);
class A {
function __construct($val) {
$this->val = $val;
}
function getClosure() {
//returns closure bound to this object and scope
return function() { return $this->val; };
}
}
$ob1 = new A(1);
$ob2 = new A(2);
$cl = $ob1->getClosure();
echo $cl(), "\n"; //1
$cl = $cl->bindTo($ob2);
echo $cl(), "\n"; //2
function helloGoodbye() {
yield "Hello";
yield " ";
yield "World!";
return "Goodbye Moon!";
}
$gen = helloGoodbye();
foreach ($gen as $value) {
echo $value; // Hello World!
}
echo $gen->getReturn(); // Goodbye Moon!
Context-Sensitive Lexer
callable and include function trait global include_once if extends goto throw endswitc implements instanceof array finally static insteadof print for abstract
interface echo foreach final namespace require declare
public new require_once case protected or return do
private xor else while const try elseif as enddeclare use default catch endfor var break die endforeach exit continue self endif list switch parent endwhile clone yield class
class foo {
function foo() {
echo 'I am the constructor';
}
}
// Pre PHP 7 code
class Logger
{
public function log($msg)
{
echo $msg;
}
}
$util->setLogger(new Logger());
// PHP 7+ code
$util->setLogger(new class {
public function log($msg)
{
echo $msg;
}
});
function hinted(bool $a, float $b, int $c, string $c)
{
}
hinted(true, 4.35, 123, "foo");
function divide(int $a, int $b): int
{
return $a / $b;
}