Whilst developing an object-orientated HMVC that has a super-object, at some point during the application process, it required the utilisation of namespaces. Here, namespaces will act as a method of "versioning" different code that can be accessed the same way. In the scaled down example below, I am able to execute the class Foo with the method qux if I am in version A or B. I understand that if I utilise self:: rather than $this the problem will disappear, however, I wish to avoid this. At the moment, I get the following PHP error:
Fatal error: Using $this when not in object context
So my question is, how can I use $this in this particular context?
namespace
{
$gamma = new \Gamma();
$gamma->execute('A', 'Foo', 'qux');
// ...
class Alpha
{
// ...
}
class Beta extends Alpha
{
public function foo($input)
{
echo $this->bar($input);
}
public function bar($input)
{
return $input;
}
}
class Gamma extends Beta
{
public function execute($space, $class, $method)
{
call_user_func_array(array($space . '\\' . $class, $method), array());
}
}
}
namespace A
{
class Foo extends \Gamma
{
public function qux()
{
$this->foo('I like turtles');
}
}
}
namespace B
{
class Foo extends \Gamma
{
public function qux()
{
$this->foo('I like strawberries');
}
}
}
The expected output is:
"I like turtles"
Any advice, answers, guidance are much appreciated. :3
Solved.
I was passing the class via the call_user_func_array function statically. Therefore, I was unable to use $this. Thus, an initiation of the requested class would be required, and passed through as a variable, like so:
// ... Continuing from Beta::execute() ...
$class = $space . '\\' . $class;
$class = new $class();
call_user_func_array(array($class, $method), array());
This is what abstract methods are for.
You should declare Master as an abstract class, and qux as an abstract method:
abstract class Master
{
public function __construct()
{
$this->qux();
}
abstract public function qux();
}
class Foo extends Master
{
public function qux()
{
....
}
}
Related
I have two classes: Action and MyAction. The latter is declared as:
class MyAction extends Action {/* some methods here */}
All I need is method in the Action class (only in it, because there will be a lot of inherited classes, and I don’t want to implement this method in all of them), which will return classname from a static call. Here is what I’m talking about:
Class Action {
function n(){/* something */}
}
And when I call it:
MyAction::n(); // it should return "MyAction"
But each declaration in the parent class has access only to the parent class __CLASS__ variable, which has the value “Action”.
Is there any possible way to do this?
__CLASS__ always returns the name of the class in which it was used, so it's not much help with a static method. If the method wasn't static you could simply use get_class($this). e.g.
class Action {
public function n(){
echo get_class($this);
}
}
class MyAction extends Action {
}
$foo=new MyAction;
$foo->n(); //displays 'MyAction'
Late static bindings, available in PHP 5.3+
Now that PHP 5.3 is released, you can use late static bindings, which let you resolve the target class for a static method call at runtime rather than when it is defined.
While the feature does not introduce a new magic constant to tell you the classname you were called through, it does provide a new function, get_called_class() which can tell you the name of the class a static method was called in. Here's an example:
Class Action {
public static function n() {
return get_called_class();
}
}
class MyAction extends Action {
}
echo MyAction::n(); //displays MyAction
Since 5.5 you can use class keyword for the class name resolution, which would be a lot faster than making function calls. Also works with interfaces.
// C extends B extends A
static::class // MyNamespace\ClassC when run in A
self::class // MyNamespace\ClassA when run in A
parent::class // MyNamespace\ClassB when run in C
MyClass::class // MyNamespace\MyClass
It's not the ideal solution, but it works on PHP < 5.3.0.
The code was copied from septuro.com
if(!function_exists('get_called_class')) {
class class_tools {
static $i = 0;
static $fl = null;
static function get_called_class() {
$bt = debug_backtrace();
if (self::$fl == $bt[2]['file'].$bt[2]['line']) {
self::$i++;
} else {
self::$i = 0;
self::$fl = $bt[2]['file'].$bt[2]['line'];
}
$lines = file($bt[2]['file']);
preg_match_all('/([a-zA-Z0-9\_]+)::'.$bt[2]['function'].'/',
$lines[$bt[2]['line']-1],
$matches);
return $matches[1][self::$i];
}
}
function get_called_class() {
return class_tools::get_called_class();
}
}
Now (when 5.3 has arrived) it's pretty simple:
http://php.net/manual/en/function.get-called-class.php
class MainSingleton {
private static $instances = array();
private static function get_called_class() {
$t = debug_backtrace();
return $t[count($t)-1]["class"];
}
public static function getInstance() {
$class = self::get_called_class();
if(!isset(self::$instances[$class]) ) {
self::$instances[$class] = new $class;
}
return self::$instances[$class];
}
}
class Singleton extends MainSingleton {
public static function getInstance()
{
return parent::getInstance();
}
protected function __construct() {
echo "A". PHP_EOL;
}
protected function __clone() {}
public function test() {
echo " * test called * ";
}
}
Singleton::getInstance()->test();
Singleton::getInstance()->test();
(PHP 5 >= 5.3.0, PHP 7)
get_called_class — The "Late Static Binding" class name
<?php
class Model
{
public static function find()
{
return get_called_class();
}
}
class User extends Model
{
}
echo User::find();
this link might be helpfull
There is no way, in the available PHP versions, to do what you want. Paul Dixon's solution is the only one. I mean, the code example, as the late static bindings feature he's talking about is available as of PHP 5.3, which is in beta.
I am trying to extend a package with my own functional ability. But the package code has type hints in the function calls, to other classes that are part of the package.
I am just looking for a way to modify the code.
More details about what I'm trying to do at
https://laracasts.com/discuss/channels/general-discussion/type-hint-hell
I have tried changing the code to use interfaces and abstracts, but i cant seem to prevent the "Declaration of class .... must be compatible with"error.
This is what i'm trying to do in a nutshell.
The package has this type of setup.
class ClassA {}
class ClassB {
public function makeClassA(ClassA $classA) : ClassA
{
return $classA;
}
}
This is what I am trying to do.
class ClassANew {}
class ClassC extends ClassB {
public function makeClassA(ClassANew $classA) : ClassANew
{
return $classA;
}
}
I get the following error,
"PHP Fatal error: Declaration of ClassC::makeClassA(ClassANew $classA): ClassANew must be compatible with ClassB::makeClassA(ClassA $classA): ClassA"
I know I could just fork the code and strip out the locked classA from ClassB, but I was trying not to do that.
If i was going to fork the code, I looked at how to maintain the premise of the original code. So, I tried changing the ClassA references in ClassB to a ClassAInterface, but I get the same error.
Is what I'm trying to do possible?
No, this is not possible to do.
Look here, for the reasons: Why is overriding method parameters a violation of strict standards in PHP?
this is a little trick, but its peculiarity that does not give rise to safety problems, in the past or already used and tested.
I know it's not really what you needed but it solves your problem to the full while maintaining the safety and the forcing of the returns of the methods
class ClassA {}
class ClassB {
public function makeClassA_ClassB(ClassA $classA) : ClassA
{
return $classA;
}
function __call($function_name, $argument){
if ($function_name==="makeClassA" && $argument[0] instanceof ClassA ) return $this->makeClassA_ClassB($argument[0]);
}
}
class ClassANew {}
class ClassC extends ClassB {
public function makeClassA_ClassC(ClassANew $classA) : ClassANew
{
return $classA;
}
function __call($function_name, $argument){
if ($function_name==="makeClassA" && $argument[0] instanceof ClassANew ) return $this->makeClassA_ClassC($argument[0]);
}
}
$t=new ClassC();
$t2=new ClassANew();
var_dump($t->makeClassA($t2)); // object(ClassANew)#212 (0) { }
$t=new ClassB();
$t2=new ClassA();
var_dump($t->makeClassA($t2)); // object(ClassA)#212 (0) { }
Ok, so I finally figured out the problem. I had to keep the original referenced return types the same. After that it works fine now.
namespace Original;
class ClassExtra {}
class ClassA {
public function __construct($container, ClassB $classB) {}
}
class ClassB {
public function __construct(ClassExtra $classExtra) {}
}
class ClassC {
public $classB;
public $containers;
public function __construct(ClassB $classB) {
$this->classB = $classB;
}
public function container(string $container = 'default'): ClassA
{
$this->containers[$container] = new ClassA($container, $this->classB);
return $this->containers[$container];
}
}
Namespace Changes;
Class NewClassA extends \Original\ClassA {}
Class NewClassB extends \Original\ClassB {}
Class NewClassC extends \Original\ClassC {
public function container(string $container = 'default'): \Original\ClassA
{
$this->containers[$container] = new NewClassA($container, $this->classB);
return $this->containers[$container];
}
}
$classC = new \Original\ClassC(new \Original\ClassB(new \Original\ClassExtra()));
var_dump(get_class($classC->container('test')));
/* string(15) "Original\ClassA" */
$classC = new NewClassC(new NewClassB(new \Original\ClassExtra()));
var_dump(get_class($classC->container('test')));
/* string(17) "Changes\NewClassA" */
That should be possible:
// just an example
class ClassA {} // the class within the used package
class ClassANew {} // your own class
// ClassB is the parent class, the one in the package
class ClassC extends ClassB {
public function makeClassA(ClassA|ClassANew $classA)
{
return $classA;
}
}
Make sure that each class/interface has correct inheritance.
More details: https://www.php.net/manual/en/language.oop5.variance.php
However, you MUST NOT totally change the logic, of the parent's function. There is an extend/implementation of the object for some reason.
If you want to just have a method that shares only the name, do not implement the new object as an child class of the previous class. The class should be only inherited from the previous class if it shares the purpose.
An example, of what I am trying to say:
There is no meaning to make a class 'Countie' that implements 'Countable' just to have a method 'count':
class Countie implements \Countable {
private int $num = 0;
public function count() {
foreach ($i = 1; $i <= $this->num; $i++) {
echo $i;
}
}
}
class TopParent
{
protected function foo()
{
$this->bar();
}
private function bar()
{
echo 'Bar';
}
}
class MidParent extends TopParent
{
protected function foo()
{
$this->midMethod();
parent::foo();
}
public function midMethod()
{
echo 'Mid';
}
public function generalMethod()
{
echo 'General';
}
}
Now the question is if I have a class, that extends MidParent because I need to call
class Target extends MidParent
{
//How to override this method to return TopParent::foo(); ?
protected function foo()
{
}
}
So I need to do this:
$mid = new MidParent();
$mid->foo(); // MidBar
$taget = new Target();
$target->generalMethod(); // General
$target->foo(); // Bar
UPDATE
Top parent is ActiveRecord class, mid is my model object. I want to use model in yii ConsoleApplication. I use 'user' module in this model, and console app doesn't support this module. So I need to override method afterFind, where user module is called. So the Target class is the class that overrides some methods from model which uses some modules that console application doesn't support.
Try this (http://php.net/manual/en/language.oop5.final.php - not allow to overriding in the childrens):
final protected function foo()
{
$this->midMethod();
parent::foo();
}
in class MidParent and the class Target can't overrides this method.
Directly - you can't. This is how OOP works.
You can do it by a little redesign, e.g. in MidParent add method:
protected function parentFoo()
{
parent::foo();
}
and in Target:
public function foo()
{
$this->parentFoo();
}
But, again, this is only a workaround to solve your question and not a solution.
Actually, you can do this like this way with Reflection::getParentClass():
class Foo
{
public function test($x, $y)
{
echo(sprintf('I am test of Foo with %s, %s'.PHP_EOL, $x, $y));
}
}
class Bar extends Foo
{
public function test()
{
echo('I am test of Bar'.PHP_EOL);
parent::test();
}
}
class Baz extends Bar
{
public function test()
{
$class = new ReflectionClass(get_class($this));
return call_user_func_array(
[$class->getParentClass()->getParentClass()->getName(), 'test'],
func_get_args()
);
}
}
$obj = new Baz();
$obj->test('bee', 'feo'); //I am test of Foo with bee, feo
-but this is an architecture smell in any case. If you need something like this, that should tell you: you're doing something wrong. I don't want to recommend anyone to use this way, but since it's possible - here it is.
#AnatoliyGusarov, your question is interesting and in a sense you can achieve what you desire using yii and php advances features like Traits and Traits in Yii.
Given that it depends on what version of php you are using.However in yii you can achieve this by behaviors and check this SOQ.
In a nutshell you have to use language advanced features or YII framework features to come around this kind of issues,but that boils down to actual requirements
I'm wondering how to force sub classes to implement a given interface method.
Let's say I have the following classes :
interface Serializable
{
public function __toString();
}
abstract class Tag // Any HTML or XML tag or whatever like <div>, <p>, <chucknorris>, etc
{
protected $attributes = array();
public function __get($memberName)
{
return $this->attributes[$member];
}
public function __set($memberName, $value)
{
$this->attributes[$memberName] = $value;
}
public function __construct() { }
public function __destruct() { }
}
I would like to force any sub class of "Tag" to implement the "Serializable" interface. For example, if i a "Paragraph" class, it would look this way :
class Paragraph extends Tag implements View
{
public function __toString()
{
print '<p';
foreach($this->attributes as $attribute => $value)
print ' '.$attribute.'="'.$value.'"';
print '>';
// Displaying children if any (not handled in this code sample).
print '</p>';
}
}
How can i force a developer to make his "Paragraph" class implement the methods from the interface "Serializable"?
Thanks for taking the time to read.
Just have the abstract class implement the interface:
interface RequiredInterface
{
public function getName();
}
abstract class BaseClass implements RequiredInterface
{
}
class MyClass extends BaseClass
{
}
Running this code will result in the error:
Fatal error: Class MyClass contains 1 abstract method and must
therefore be declared abstract or implement the remaining methods
(RequiredInterface::getName)
This requires the developer to code the methods of RequiredInterface.
PHP code example:
class Foo {
public function sneeze() { echo 'achoooo'; }
}
abstract class Bar extends Foo {
public abstract function hiccup();
}
class Baz extends Bar {
public function hiccup() { echo 'hiccup!'; }
}
$baz = new Baz();
$baz->sneeze();
$baz->hiccup();
It is possible for an abstract class to extend Serializable, as abstract classes do not need to be base classes
This adds a __constructor to your class Paragraph which checks to see if Serializable is implemented.
class Paragraph extends Tag implements View
{
public function __construct(){
if(!class_implements('Serializable')){
throw new error; // set your error here..
}
}
public function __toString()
{
print '<p';
foreach($this->attributes as $attribute => $value)
print ' '.$attribute.'="'.$value.'"';
print '>';
// Displaying children if any (not handled in this code sample).
print '</p>';
}
}
I'm writing a unit test for a class method that calls another class's method using a mock, only the method that needs to be called is declared as final, so PHPUnit is unable to mock it. Is there a different approach I can take?
example:
class to be mocked
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
my test case
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$mock = $this->getMock('Class_To_Mock', array('needsToBeCalled'));
$mock->expects($this->once())
->method('needsToBeCalled')
->with($this->equalTo(array('option'));
}
}
Edit: If using the solution provided by Mike B and you have a setter/getter for the object you're mocking that does type checking (to ensure the correct object was passed into the setter), you'll need to mock the getter on the class you're testing and have it return the other mock.
example:
class to be mocked
class Class_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
mock
class Class_To_MockMock
{
public function needsToBeCalled($options)
{
...
}
}
class to be tested
class Class_To_Be_Tested
{
public function setClassToMock(Class_To_Mock $classToMock)
{
...
}
public function getClassToMock()
{
...
}
public function doSomething()
{
$this->getClassToMock()
->needsToBeCalled(array('option'));
}
}
my test case
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$classToTest = $this->getMock('Class_To_Be_Tested', array('getClassToMock'));
$mock = $this->getMock('Class_To_MockMock', array('needsToBeCalled'));
$classToTest->expects($this->any())
->method('getClassToMock')
->will($this->returnValue($mock));
$mock->expects($this->once())
->method('needsToBeCalled')
->with($this->equalTo(array('option'));
$classToTest->doSomething();
}
}
I don't think PHPUnit supports stubbing/mocking of final methods. You may have to create your own stub for this situation and do some extension trickery:
class myTestClassMock {
public function needsToBeCalled() {
$foo = new Class_To_Mock();
$result = $foo->needsToBeCalled();
return array('option');
}
}
Found this in the PHPUnit Manual under Chapter 11. Test Doubles
Limitations
Please note that final, private and static methods cannot be stubbed or mocked. They are ignored by PHPUnit's test double functionality and retain their original behavior.
I just stumbled upon this issue today. Another alternative is to mock the interface that the class implements, given that it implements an interface and you use the interface as type hinting.
For example, given the problem in question, you can create an interface and use it as follows:
interface Interface_To_Mock
{
function needsToBeCalled($options);
}
class Class_To_Mock implements Interface_To_Mock
{
final public function needsToBeCalled($options)
{
...
}
}
class Class_To_Be_Tested
{
public function setClassToMock(Interface_To_Mock $classToMock)
{
...
}
...
}
class MyTest extends PHPUnit_Framework_TestCase
{
public function testDoSomething()
{
$mock = $this->getMock('Interface_To_Mock', array('needsToBeCalled'));
...
}
}