$a() - $b()
<?php
class N {
private int $n;
public function __construct(int $n = 0) {
$this->n = $n;
}
public function __invoke(): int {
return $this->n;
}
}
$a = new N(5);
$b = fn() => 100;
var_dump($a() - $b()); // -95
<?php
class N {
private int $n;
public function __construct(int $n = 0) {
$this->n = $n;
}
public function __invoke(int $multiplier = 1): int {
return $multiplier * $this->n;
}
}
$a = new N(5);
$b = fn(int $multiplier = 1) => $multiplier * 100;
var_dump($a(100) - $b()); // 400
sandbox.onlinephpfunctions.com/code/331eef7f9ad530...<?php
interface NumericInterface {
public function add(Numeric $second): NumericInterface;
public function toInt(): int;
}
class Numeric implements NumericInterface {
private $n;
public function __construct($n = 0) {
$this->n = $n;
}
public function add(Numeric $second): self {
return new self($second->n + $this->n);
}
public function toInt(): int {
return $this->n;
}
}
$num1 = new Numeric(10);
$num2 = new Numeric(18);
$result = $num1->add($num2)->toInt();
var_dump($result); // 28
func main() {
forever := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
go func(ctx context.Context) {
for {
select {
case <-ctx.Done(): // if cancel() execute
forever <- struct{}{}
return
default:
fmt.Println("for loop")
}
time.Sleep(500 * time.Millisecond)
}
}(ctx)
go func() {
time.Sleep(3 * time.Second)
cancel()
}()
<-forever
fmt.Println("finish")
}
$curl->tick()
, чтобы он мог продолжить работу. wait() под капотом делает именно этоuse GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlMultiHandler;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\ResponseInterface;
$curl = new CurlMultiHandler;
$handler = HandlerStack::create($curl);
$client = new Client(['handler' => $handler]);
$p = $client
->getAsync('http://google.com')
->then(
function (ResponseInterface $res) {
echo 'response: ' . $res->getStatusCode() . PHP_EOL;
},
function (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
);
while ($p->getState() === 'pending') {
$curl->tick();
// sleep(1);
}
echo 'bottom' . PHP_EOL;
Почему codeception selenium морочит голову
Или она должна не работать
Или должна работать
<?php
$catalog = new stdClass();
$catalog->product = new stdClass();
$catalog->product->item = 'Short';
$catalog->product->model = 'Adidas';
$catalogPaths = ['product.item', 'product.model'];
function extractValuesByPath(object $obj, array $paths) {
return array_reduce($paths, function(array $values, string $pattern) use($obj) {
$values[] = extractPathValue($obj, $pattern);
return $values;
}, []);
}
function extractPathValue($obj, $pattern) {
$paths = explode('.', $pattern);
foreach($paths as $path) {
if (!property_exists($obj, $path)) {
// Можно нулл вернуть или к пример `undefined $path`
throw new InvalidArgumentException('Нет такого свойства во входящих данных!');
}
$obj = $obj->$path;
}
return $obj;
}
$res = extractValuesByPath($catalog, $catalogPaths);
var_dump($res);
// [
// 'Short',
// 'Adidas',
// ]
/**
* @OneToMany(targetEntity="Article", mappedBy="topic", cascade={"persist", "remove"})
*/
private $articles;
/search/{region}/{city}
и /search
— разные роуты/**
* @Route("/search")
* @ParamConverter("region", converter="querystring")
* @ParamConverter("city", converter="querystring")
*/
public function index(string $region, string $city)
{
return new Response(print_r($name, true));
}
class QueryStringConverter implements ParamConverterInterface
{
public function supports(ParamConverter $configuration)
{
return 'querystring' == $configuration->getConverter();
}
public function apply(Request $request, ParamConverter $configuration)
{
$param = $configuration->getName();
if (!$request->query->has($param)) {
return false;
}
$value = $request->query->get($param);
$request->attributes->set($param, $value);
}
}
разным ролям нужны сильно различающиеся наборы полей
затем сохраняю это через CrudRepository, получается в объект Policy теперь есть id