Suppose we have a class. We create an object from the class and when we do the class Extends himself base on the object initialization value..
For example:
$objectType1 = new Types(1);
$objectType1->Activate(); // It calls an activation function for type 1
$objectType2 = new Types(2);
$objectType2->Activate(); // It calls an activation function for type 2
I don't want to use the standard procedure of class extending:
class type1 extends types{}
You cannot extend a class at runtime. Use an instance variable to distinct the two type or use a factory.
Example for instance variable:
class Types() {
private $type;
public function __construct($type) {
$this->type = $type;
}
public function activate() {
if($this->$type == 1) {
// do this
}
else if($this->type == 2) {
// do that
}
}
}
Example for factory pattern:
abstract class BaseClass {
// Force Extending class to define this method
abstract public function activate();
// Common method
public function printOut() {
echo "Hello World";
}
}
class Type1 extends BaseClass {
public function activate() {
// do something
}
}
class Type2 extends BaseClass {
public function activate() {
// do something else
}
}
class TypeFactory {
public static function getType($tpye) {
if($type == 1) {
return new Type1();
}
else if($type == 2) {
return new Type2();
}
}
}
then you do:
$obj = TypeFactory::getType($1);
$obj->activate();
Update:
Since PHP 5.3 you can use anonymous functions. Maybe you can make use of this.
Related
I am writing a basic class with some call_user_func() and then derived class with a method I want to be called from said call_user_func(). It looks as follows:
class Basic
{
private $InputHandler = null;
public function SetNewHandler(callable $NewHandler)
{
$this->InputHandler = $NewHandler;
}
public function ProcessInput()
{
call_user_func(array($this,$this->InputHandler));
}
}
class Specific extends Basic
{
public function Handler()
{
echo "Handler() is called\n";
}
}
$spec = new Specific();
$spec->SetNewHandler('Specific\Handler');
$spec->ProcessInput();
unset($spec);
Obviously it does not work since Specific\Handler is not recognized as a valid callable.
But what is the proper way to achieve this outcome? Parent class should be oblivious to the details of implementation of derived class and\or handler method.
I use php 7.4
I would just create a default handler method in the base class:
class Base
{
public function ProcessInput()
{
$this->Handler();
}
public function Handler()
{
// Some default code or just leave empty
echo "Base Handler() is called\n";
}
}
class Specific extends Base
{
public function Handler()
{
echo "Specific Handler() is called\n";
}
}
$spec = new Specific;
$spec->ProcessInput();
This way you don't need to manually bind the handler you want to use for each instance (which could be cumbersome). If you want a new handler, create a new class with that handler.
Here's a demo
If you made the call with call_user_func(array ($this, $this->InputHandler)); a method name is expected as the 2nd array element.
class Basic
{
private $InputHandler = null;
public function SetNewHandler($NewHandler)
{
$this->InputHandler = $NewHandler;
}
public function ProcessInput()
{
call_user_func(array($this,$this->InputHandler));
}
}
class Specific extends Basic
{
public function Handler()
{
echo "Handler() is called\n";
}
}
$spec = new Specific();
$spec->SetNewHandler('Handler');
$spec->ProcessInput();
Instead of call_user_func(Array($this, $this->InputHandler)); does this work here too:
$method = $this->InputHandler;
$this->$method();
or this:
$this->{$this->InputHandler}();
Alternatively: A callable is passed similarly as described by #Magnus Eriksson in the comment.
class Basic
{
private $InputHandler = null;
public function SetNewHandler(callable $NewHandler)
{
$this->InputHandler = $NewHandler;
}
public function ProcessInput()
{
call_user_func($this->InputHandler);
}
}
class Specific extends Basic
{
public function Handler()
{
echo "Handler() is called\n";
}
}
$spec = new Specific();
$spec->SetNewHandler([$spec,'Handler']);
$spec->ProcessInput();
The magical __call() and __callStatic can pretty much handle any non existing method on the class, but is there a way to handle a non existing magical method on a class?!
Here's an example on why I need this:
I have a class called DoSomething:
class DoSomething{
public function ok(){
echo 'Something!';
}
}
I want to call this class as a function for a reason! which should call the __invoke function of that class:
$doSomething = new DoSomething();
$doSomething();
Normally by doing that, the class should look for the __invoke function, however in my case I don't to have that function declared on my class (DoSomething), instead I want to be able to call another function (such as the ok()) if the __invoke doesn't exist.
I was expecting something like this to work, but of course it didn't :)
public function __call($class, $arguments)
{
$object = IoC::resolve($class);
$object->ok(...$arguments);
}
The main goal is to use the class as a function, without having to declare the __invoke method. Handle the function does not exist error and call another function instead.
I think that would be really cool :D I appreciate suggestions or other solutions to achieve this.
Internal solution
Extract an abstract class
You could extract an abstract class and have your classes extend it:
<?php
abstract class Invokable
{
public function __invoke()
{
return $this->ok();
}
abstract public function ok();
}
class DoSomething extends Invokable
{
public function ok()
{
echo 'Something';
}
}
$doSomething = new DoSomething();
echo $doSomething();
For an example, see:
https://3v4l.org/m0ih8
Extract a trait
You could extract a trait and have your classes use it:
<?php
trait InvokableTrait
{
public function __invoke()
{
return $this->ok();
}
}
class DoSomething
{
use InvokableTrait;
public function ok()
{
echo 'Something';
}
}
$doSomething = new DoSomething();
echo $doSomething();
For an example, see:
https://3v4l.org/ftUfI
External Solution
Create a proxy
You could create a proxy (a decorator) that composes the object that is not invokable:
<?php
class InvokableDecorator
{
private $decorated;
public function __construct($decorated)
{
$this->decorated = $decorated;
}
public function __call($name, $arguments)
{
/**
* delegate to decorated object if the method exists
*/
if (method_exists($this->decorated, $name)) {
return $this->decorated->{$name}($arguments);
}
}
public function __invoke()
{
return $this->decorated->ok();
}
}
class DoSomething
{
public function ok()
{
echo 'Something';
}
}
$doSomething = new InvokableDecorator(new DoSomething());
echo $doSomething();
For an example, see:
https://3v4l.org/C3XEX
Create a handler
You could create a handler that takes care of determining this externally:
<?php
class Handler
{
public function handle($subject)
{
if (is_callable($subject)) {
return $subject();
}
if (method_exists($subject, 'ok')) {
return $subject->ok();
}
throw new \BadMethodCallException(sprintf(
'Unable to handle instance of "%s"',
get_class($subject)
));
}
}
class DoSomething
{
public function ok()
{
echo 'Something';
}
}
$handler = new Handler();
echo $handler->handle(new DoSomething());
For an example, see:
https://3v4l.org/E0NVs
Whats wrong with me OOP here.
I want to inherit from Class A
The return_output method will do something common so I don't want to write that in the inherited classes.
However when I do B->return_output() I want it to run the do_something method in Class B, but I see that it always runs the method from Class A.
Should I replace $this with something else?
class A {
private function do_something() {
// do something
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
private function do_something() {
// do something different
}
}
var newClass = new B;
echo B->return_output();
use protected and not private since you are running it inside of scope a and scope b can't access private scope a:
class A {
protected function do_something() {
echo('ado_something');
}
public function return_output() {
$op = $this->do_something();
// add some wrappers to $op
return $op;
}
}
class B extends A {
protected function do_something() {
echo('bdo_something');
}
}
$newClass = new B;
echo $newClass->return_output();
If you have something like the following:
abstract class User {
function checkThis() {
return true;
}
function mainFunction() {
}
}
and you want to call checkThis() from within mainFunction(), would you do it like this?
abstract class User {
function checkThis() {
return true;
}
function mainFunction() {
if ($this->checkThis())
echo "Works";
}
I seem to be having problems if I call this from within another class, say:
class SecondClass {
function test() {
User::mainFunction();
}
}
Provides this error:
Fatal error: Call to undefined method SecondClass::checkThis()
You should maybe consider having a class you can instantiate which extends this abstract class. e.g.
class userConcrete extends User {
}
Then in second class you can inject the user object like so:
class SecondClass{
protected $user;
public function __construct(userConcrete $user)
{
$this->user = $user;
}
function test(){
$this->user->mainFunction();
}
}
Also making mainFunction public in your abstract class.
I'm trying to test a private method in an abstract class.
I've got three abstract classes:
abstract class AbstractClass1 extends AbstractClass2
{
private function _privateFunction()
{
//method's body
}
}
abstract class AbstractClass2 extends AbstractClass3
{
public function __construct($param)
{
parent::__construct($param)
}
}
abstract class AbstractClass3
{
public function __construct($param = array())
{
//something
}
}
The test class:
class AbstractClass1Test extends PHPUnit_Framework_TestCase
{
public function test_privateFunction()
{
$stub = $this->getMockForAbstractClass("AbstractClass1");
$class = new ReflectionClass($stub);
$method = $class->getMethod("_privateFunction");
$method->setAccessible(true);
//some assertings with $method->invoke($stub)
}
}
The test failed, because of the error:
Missing argument 1 for AbstractClass2::__construct(), called in /usr/share/php/PHPUnit/Framework/MockObject/Generator.php on line 190 and defined
AbstractClass2.php
public function __construct($param)
AbstractClass1.php
$classMock = $this->getMockForAbstractClass("AbstractClass1");
Generator.php:190
if ($callOriginalConstructor &&
!interface_exists($originalClassName, $callAutoload)) {
if (count($arguments) == 0) {
<strong>$mockObject = new $mock['mockClassName'];</strong>
} else {
$mockClass = new ReflectionClass($mock['mockClassName']);
$mockObject = $mockClass->newInstanceArgs($arguments);
}
} else ...
What do I wrong? Or how can I test my private function in this situation?
You need to pass an argument to AbstractClass1's constructor. Pass constructor arguments in an array as the second argument to getMockForAbstractClass().
$stub = $this->getMockForAbstractClass("AbstractClass1", array('param'));
Seeing as you overrode the original constructor,
public function __construct($param = array()) //Allow null $param as it would default to array();
With a new one:
public function __construct($param) //Does not allow null $param.
You will require to define the $param when you initialize the object. That's probably your problem.
Objects in PHP are not like JavaScript, they cannot be called like associative arrays. Your object initialization should look like:
$mockObject = new ClassExtendingAbstractClass1Or2('parameter');
The new keyword cannot be used in front of a variable.