e preg_replace PHP, . , , . deprecated PHP 5.5. preg_replace_callback. RFC.strval, intval floatval. boolval. , (bool), .IntlCalendar, IntlGregorianCalendar, IntlTimeZone, IntlBreakIterator, IntlRuleBasedBreakIterator, IntlCodePointBreakIterator. intl, , , Calendar BreakIterator.array_column ( array_pluck), :<?php
$userNames = array_column($users, 'name');
//
$userNames = [];
foreach ($users as $user) {
$userNames[] = $user['name'];
}
sha1 .crypt. , , API :<?php
$password = "foo";
//
$hash = password_hash($password, PASSWORD_BCRYPT);
//
if (password_verify($password, $hash)) {
// !
} else {
// !
}
<?php
function randomHexString($length) {
$str = '';
for ($i = 0; $i < $length; ++$i) {
$str .= "0123456789abcdef"[mt_rand(0, 15)]; // dereference
}
}
function randomBool() {
return [false, true][mt_rand(0, 1)]; // dereference
}
empty() , . , empty($this->getFriends()) . PHP 5.5 . . RFC.<?php
use Some\Deeply\Nested\Namespace\FooBar;
// , `FooBar`
$reflection = new ReflectionClass('FooBar');
FooBar::class, :<?php
use Some\Deeply\Nested\Namespace\FooBar;
// , FooBar::class "Some\\Deeply\\Nested\\Namespace\\FooBar"
$reflection = new ReflectionClass(FooBar::class);
function create_query($where, $order_by, $join_type='', $execute = false, $report_errors = true) { ... }$report_errors = false . :create_query("deleted=0", "name", default, default, false);123, 123.0, "123" int , " " . .function foo(int $i) { ... }
foo(1); // $i = 1
foo(1.0); // $i = 1
foo("1"); // $i = 1
foo("1abc"); // , $i = 1 notice
foo(1.5); // , $i = 1 notice
foo([]); //
foo("abc"); //
getXYZ() setXYZ($value), . , , :<?php
class TimePeriod {
public $seconds;
public $hours {
get { return $this->seconds / 3600; }
set { $this->seconds = $value * 3600; }
}
}
$timePeriod = new TimePeriod;
$timePeriod->hours = 10;
var_dump($timePeriod->seconds); // int(36000)
var_dump($timePeriod->hours); // int(10)
range :<?php
function *xrange($start, $end, $step = 1) {
for ($i = $start; $i < $end; $i += $step) {
yield $i;
}
}
foreach (xrange(10, 20) as $i) {
// ...
}
xrange , range : , , .$firstNames = [foreach ($users as $user) yield $user->firstName];$firstNames = [];
foreach ($users as $user) {
$firstNames[] = $user->firstName;
}
$underageUsers = [foreach ($users as $user) if ($user->age < 18) yield $user];Source: https://habr.com/ru/post/147674/
All Articles