Támogatónk:
sssss
ssss
ssss
PDO starts the DB connection as soon as it is instantiated
<?php
$cities = City::get();
foreach ($cities as $city) {
$city->country->name;
}
<?php
$cities = City::with("country")->get();
Sign | Name | Good/Bad |
---|---|---|
O(1) | Constant Time | Perfect |
O(log n) | Logarithmic Time | Very good |
O(n) | Linear Time | Good |
O(n^2) | Quadratic Time | Avoid |
2^O(n) | Exponential Time | Panic |
<?php
$numbers = [1, 2, 3, 4, 5];
echo $numbers[2];
<?php
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number;
}
<?php
$numbers1 = [1, 2, 3, 4, 5];
$numbers2 = [3, 4, 5, 6, 7];
foreach ($numbers1 as $i) {
foreach ($numbers2 as $j) {
if ($i === $j) {
echo $i;
}
}
}
<?php
$numbers1 = [1, 2, 3, 4, 5];
$numbers2 = [3, 4, 5, 6, 7];
// [3 => 0, 4 => 1, 5 => 2, 6 => 3, 7 => 4]
$numbers2Map = array_flip($numbers2);
foreach ($numbers1 as $number) {
if (isset($numbers2Map[$number])) {
echo $number;
}
}
<?php
$numbers1 = [1, 2, 3, 4, 5];
$numbers2 = [3, 4, 5, 6, 7];
// [3, 4, 5]
$numbers = array_intersect(
$numbers1,
$numbers2
);
foreach ($numbers as $number) {
echo $number;
}
<?php
$harmony = new Harmony(ServerRequestFactory::fromGlobals(), new Response());
$harmony
->addMiddleware(new HttpHandlerRunnerMiddleware(new SapiEmitter()))
->addMiddleware(new FastRouteMiddleware($router))
->addMiddleware(new DispatcherMiddleware())
->run();
[OPCACHE]
zend-extension=opcache
opcache.consistency_checks=0
opcache.enable=1
opcache.enable_cli=1
opcache.enable_file_override=1
opcache.fast_shutdown=1
opcache.max_accelerated_files=60000
opcache.memory_consumption=128
opcache.validate_timestamps=0
opcache.revalidate_freq=8
opcache.revalidate_path=0
opcache.save_comments=1
opcache.use_cwd=0
opcache.file_update_protection=0
opcache.interned_strings_buffer=32
[OPCACHE]
...
opcache.preload=/var/www/preload.php
<?php
opcache_compile_file('/var/www/src/Class1.php');
opcache_compile_file('/var/www/src/Class2.php');
opcache_compile_file('/var/www/src/Class3.php');
// ...
composer install \
--no-interaction \
--no-suggest \
--no-dev \
--no-scripts \
--prefer-dist \
--optimize-autoloader \
--classmap-authoritative
<?php
// ------------ Slower ---------------
require_once("vendor/autoload.php");
// ------------ Faster ---------------
require_once("src/Class1.php");
<?php
class Class1
{
public const $items = [
"Class1" => "/var/www" . "/src/Class1.php",
];
public static $items = [
"Class2" => __DIR__ . "/src/Class1.php",
];
}
<?php
// -------------------------------------- Slower ----------------------------------
function funct2(string $a, int $b, array $c, stdClass $d, float $e, bool $f): void
{
// ...
}
// -------------------------------------- Faster ----------------------------------
/**
* @param string $a
* @param int $b
* @param array $c
* @param stdClass $d
* @param float $e
* @param bool $f
* @return void
*/
function funct1($a, $b, $c, $d, $e, $f)
{
// ...
}
namespace App;
// ------- Slower ----------
count([]);
// ------- Faster ----------
\count();
use function count;
<?php
// ---------- Slower -------------
array_key_exists("key", $array);
// ---------- Faster -------------
isset($array["key"]);
<?php
$kitchen = new Kitchen();
// ---------------------- Slower --------------------------
$sweetsThief = new ReflectionProperty("Kitchen", "yummy");
$sweetsThief->setAccessible(true);
// ---------------------- Faster --------------------------
$sweetsThief = Closure::bind(
static function (Kitchen $kitchen) {
return $kitchen->yummy;
},
null,
"Kitchen"
);
https://blog.professorbeekums.com/performance-vs-scalability/
https://blog.blackfire.io/speeding-up-autoloading-on-php-5-6-7-0-for-everyone.html
https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/
https://steemit.com/php/@crell/php-use-associative-arrays-basically-never