Answer the question
In order to leave comments, you need to log in
How to include a class in __construct?
Hello
<?php
interface SessionInterface
{
public function put($key, $value);
public function push(string $key, $value);
}
class BaseHandler implements SessionInterface
{
public function put($key, $value)
{
echo 'put';
}
public function push(string $key, $value)
{
echo 'push';
}
}
class Session
{
public function __construct()
{
$config = 'BaseHandler';
if ($config == 'BaseHandler') {
// нужно использовать class BaseHandler
}
echo 'Session...';
}
}
$Session = new Session();
Answer the question
In order to leave comments, you need to log in
class Session
{
protected $handler;
public function __construct(SessionInterface $handler)
{
$this->handler = $handler;
}
}
$session = new Session(new BaseHandler);
Why didn't you like inheritance from the BaseHandler class?
<?php
interface SessionInterface
{
public function put($key, $value);
public function push(string $key, $value);
}
class BaseHandler implements SessionInterface
{
public function put($key, $value)
{
echo 'put';
}
public function push(string $key, $value)
{
echo 'push';
}
}
class Session extends BaseHandler
{
public function __construct()
{
echo 'Session...';
}
}
$Session = new Session();
// Можно использовать методы class BaseHandler
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question