I would like to write a generic method that refers to a generic class (but the same method) in php.
class A {
public static function Dox(){
}
}
class B {
public static function Dox(){
}
}
class C{
public static function Include($class){
$result = $class::Dox(); //instead of 2 methods => A::Dox and B::Dox
}
}
I get an error.
any suggestions?
include is a keyword. Rename your method to foo(), bar() or anything that is not a keyword.
e.g.
<?php
class A {
public static function Dox() { return 'A::Dox'; }
}
class B {
public static function Dox() { return 'B::Dox'; }
}
class C {
public static function foo($class) {
$result = $class::Dox();
echo 'result: ', $result, "\n";
}
}
foreach( array('A','B') as $c ) {
C::foo($c);
}
prints
result: A::Dox
result: B::Dox
Does call_user_func work?
class A {
public static function Dox() {
}
}
class B {
public static function Dox() {
}
}
class C {
public static function Include($class) {
$result = call_user_func(array($class, "Dox"));
}
}
Include keyword "spesific keyword".
Try it:
public static function IncludeXXX(){...}
Related
I have a class and two functions inside it as follows:
class MyClassName
{
protected function myFunction1()
{
// some code here
return $something;
}
public function myFunction2()
{
// some code here
return $somethingElse;
}
}
What I need to do is define a variable in myFunction1() and then use it in myFunction2(). What is the best practice to do that?
class MyClassName
{
public $var = 0;
protected function myFunction1()
{
// some code here
$this->var = ...;
return $something;
}
public function myFunction2()
{
// some code here
echo $this->var;
return $somethingElse;
}
}
Actually vars should be defined out of the function and then set a value. Then can be modified over all the script, by doing this->var
Make it a class property
class MyClassName
{
private $property;
public function __construct() {
$this->myFunction1();
}
protected function myFunction1()
{
// some code here
$this->property = 'an apple';
}
public function myFunction2()
{
// some code here
return $this->property;
}
}
Now test it:
$my_class = new MyClassName();
$something = $my_class->myFunction2();
echo $something;
I have this Base class:
class Base
{
public $extA;
public $extB;
function __construct()
{
}
public function Init()
{
$this->extA = new ExtA();
$this->extB = new ExtB( $this );
}
public function Test()
{
return 'Base Test Here!';
}
}
class ExtA extending the Base Class
class ExtA extends Base
{
public function Test()
{
return 'ExtA Test Here!';
}
}
class ExtB extending the Base Class too
class ExtB extends Base
{
private $base;
public function __construct( $base )
{
$this->base = $base;
}
public function Test()
{
return 'ExtB calling ExtA->Test()::' . $this->base->extA->Test();
}
}
$base = new Base();
$base->Init();
var_dump( $base->Test() );
var_dump( $base->extA->Test() );
var_dump( $base->extB->Test() );
I try to call the ExtA class Test() function from the ExtB,
both of ExtA and ExtB is exnteding the Base class.
My question is : is this ok, or have a better, faster solution for this?
The extends is necessary too?
Or simply enough like this
class ExtA
{
...
}
class ExtB
{
...
}
Thanks!
This is weird way of OOP.
The Base class should not know anything about its children so we shall go more correct way. Let's implement Decorator pattern:
interface IExt
{
public function test();
}
abstract class ExtDecorator implements IExt
{
protected $instance;
public function __construct(IExt $ext)
{
$this->instance = $ext;
}
}
class ExtA extends ExtDecorator
{
public function test()
{
return 'ExtA::test here and calling... ' . $this->instance->test();
}
}
class ExtB extends ExtDecorator
{
public function test()
{
return 'ExtB::test is here and calling... ' . $this->instance->test();
}
}
class Base implements IExt
{
public function test()
{
return 'Base::test here!';
}
}
class Printer
{
public static function doMagic(IExt $ext)
{
echo $ext->test()."\n";
}
}
Printer::doMagic($base = new Base);
// Base::test here!
Printer::doMagic($extA = new ExtA($base));
// ExtA::test here and calling... Base::test here!
Printer::doMagic(new ExtB($extA));
// ExtB::test is here and calling... ExtA::test here and calling... Base::test here!
You can play further any way you want
this is my class:
class toyota extends car {
function drive() {
}
function break() {
}
}
class car {
function pre() {
}
}
Is there any way I can do so that when I run $car->drive(), $car->break() (or any other function in toyota), it would call $car->pre() first before calling the functions in toyota?
Yep. You could use protected and some __call magic:
class toyota extends car {
protected function drive() {
echo "drive\n";
}
protected function dobreak() {
echo "break\n";
}
}
class car {
public function __call($name, $args)
{
if (method_exists($this, $name)) {
$this->pre();
return call_user_func_array(array($this, $name), $args);
}
}
function pre() {
echo "pre\n";
}
}
$car = new toyota();
$car->drive();
$car->dobreak();
http://ideone.com/SGi1g
You could do the following, but I don't think that is what you want.
class toyota extends car {
function drive() {
$this->pre();
}
function break() {
$this->pre();
}
}
class car {
function pre() {
}
}
You may want to look into PHP specific magic methods. http://php.net/manual/en/language.oop5.magic.php
This will better done with the magic methods called __call()
public function __call($name, $arguments)
{
$this -> pre();
return $this -> $name($arguments);
}
What is this method? It overrides the default method call, so that preCall State can be invoked.
Your toyota class
class toyota extends car {
public function __call($name, $arguments)
{
$this -> pre();
return call_user_func_array(array($this, $name), $arguments);
}
function drive() {
}
function break() {
}
}
If you are using PHP5 (>=5.3.2), there is a solution that works with declaring all methods as private. This will enforce method call from single function call:
exec_method()
To run at: http://ideone.com/cvfCXm
The code snippet is here:
<?php
class car {
//method to get class method
public function get_method($method_name) {
$class = new ReflectionClass(get_class($this));
$method = $class->getMethod($method_name);
$method->setAccessible(true);
return $method;
}
public function exec_method($method_name, $arg_args=array()) {
//execute the pre() function before the specified method
$this->pre();
//execute the specified method
$this->get_method($method_name)->invokeArgs($this, $arg_args);
}
public function pre() {
echo 'pre';
echo '<br />';
}
}
class toyota extends car {
private function drive() {
echo 'drive';
echo '<br />';
}
private function brake() {
echo 'brake';
echo '<br />';
}
}
$toyota = new toyota();
$toyota->exec_method('drive');
$toyota->exec_method('brake');
?>
Reference:
Answer to Best practices to test protected methods with PHPUnit [closed]
Just add a constructor, like this...
class toyota extends car {
function __construct() {
$this->pre();
}
function drive() {
echo "drive!";
}
function dobreak() {
echo "break!";
}
}
class car {
function pre() {
echo "Hello!";
}
}
$car = new toyota();
$car->drive();
$car->dobreak();
Classes which have a constructor method call this method on each
newly-created object, so it is suitable for any initialization that
the object may need before it is used.
break is reserved, so you shouldn't use this as a function name.
I would like to have a base class with basic properties and functions, so I dont have to define them in all child classes.
I use php 5.3.3.
Is this impossible ?
class A {
private $debug;
private $var;
protected function setVar($str) {
$this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
protected function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string');
}
}
$myobj = new B();
$myobj->getDebug();
// expected output "Set by function `doSomething` in class `B`."
<?php
class A {
private $debug;
private $var;
protected function setVar($str) {
$this->debug = 'Set by function `'. MAGIC_HERE .'` in class `'. get_called_class() .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
// Notice the public here, instead of protected //
public function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string');
}
}
$myobj = new B();
echo $myobj->getDebug();
// expected output "Set by function `doSomething` in class `B`."
You had just two small issues. A::getDebug needed to be public to be accessible from the outside and you forgot to output the return of A::getDebug.
See the debug_backtrace function. Note this function is expensive, so you should disable those debug features in production.
Is this no good for you?
I'm not running 5.3 locally, so I had to switch out get_called_class() but you could still use it. Should have made that clear, sorry.
class A {
private $debug;
private $var;
protected function setVar($str, $class) {
$this->debug = 'Set by function `` in class `'. $class .'`.';
$this->var = $str;
return true;
}
protected function getVar() {
return $this->var;
}
public function getDebug() {
return $this->debug;
}
}
class B extends A {
public function __construct() {
$this->doSomething();
}
public function doSomething() {
$this->setVar('my string', __CLASS__);
}
}
$myobj = new B();
echo $myobj->getDebug();
Please look at the following code snipped
class A
{
function __get($name)
{
if ($name == 'service') {
return new Proxy($this);
}
}
function render()
{
echo 'Rendering A class : ' . $this->service->get('title');
}
protected function resourceFile()
{
return 'A.res';
}
}
class B extends A
{
protected function resourceFile()
{
return 'B.res';
}
function render()
{
parent::render();
echo 'Rendering B class : ' . $this->service->get('title');
}
}
class Proxy
{
private $mSite = null;
public function __construct($site)
{
$this->mSite = $site;
}
public function get($key)
{
// problem here
}
}
// in the main script
$obj = new B();
$obj->render();
Question is: in method 'get' of class 'Proxy', how I extract the corresponding resource file name (resourceFile returns the name) by using only $mSite (object pointer)?
What about:
public function get($key)
{
$file = $this->mSite->resourceFile();
}
But this requires A::resourceFile() to be public otherwise you cannot access the method from outside the object scope - that's what access modifiers have been designed for.
EDIT:
OK - now I think I do understand, what you want to achieve. The following example should demonstrate the desired behavior:
class A
{
private function _method()
{
return 'A';
}
public function render()
{
echo $this->_method();
}
}
class B extends A
{
private function _method()
{
return 'B';
}
public function render()
{
parent::render();
echo $this->_method();
}
}
$b = new B();
$b->render(); // outputs AB
But if you ask me - I think you should think about your design as the solution seems somewhat hacky and hard to understand for someone looking at the code.