Executing calling class' method - php

class Car
{
$gas= new Gas();
$gas->fill( 'filledHandler' );
function filledHandler()
{
echo 'Gas has been filled!';
}
}
class Gas
{
function fill( $function )
{
// do something here
$function();
}
}
I need to call $function of calling class. Right now, it's looking for a global function

You have to pass the calling instance.
class Car
{
function fillGas()
{
$gas = new Gas();
$gas->fill($this, 'filledHandler');
}
function filledHandler()
{
echo 'Gas has been filled!';
}
}
class Gas
{
function fill($obj, $function)
{
// If you need the class name, use get_class($obj)
$obj->$function();
}
}

class Car
{
function __construct()
{
$gas= new Gas();
$gas->fill($this, 'filledHandler' );
}
function filledHandler()
{
echo 'Gas has been filled!';
}
}
class Gas
{
function fill($object, $function )
{
$object->$function();
}
}
(Ask more question (like what exactly do you want to happen anyway) and get more answer text ^^.)

Related

How can I change existing function in a class

I have a form class and I want define different functions when form be submitted.
<?php
class Forms {
function __construct() {
if (!empty($_POST['exampleInput'])) {
$this->PostedForms();
}
}
function __call($func, $param) {
}
function PostedForms() {
if (!empty($_POST['exampleInput'])) {
if (function_exists($this->userDefined)) {
$this->userDefined();
}
}
}
}
$form = new Forms();
$form->userDefined = function ($param) {
print_r($_POST);
}
?>
I want define userDefined function outside of class. How can I do this? Can I change any function of class after class was called? Can I change userDefined = function ($param) {print_r($_GET);} for example?

php class function wrapper

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.

Find the class name of the calling function in php

Lets say I have:
class Zebra{
public static function action(){
print 'I was called from the '.get_class().' class'; // How do I get water here?
}
}
class Water{
public static function drink(){
Zebra::action();
}
}
Water::drink();
How do I get "water" from the zebra class?
(This is for php 5.3)
You can get the caller's info from debug_backtrace http://php.net/manual/en/function.debug-backtrace.php
One not so good solution is :
use __METHOD__ or __FUNCTION__ or __CLASS__ .
and pass it as parameter to function being called.
http://codepad.org/AVG0Taq7
<?php
class Zebra{
public static function action($source){
print 'I was called from the '.$source.' class'; // How do I get water here?
}
}
class Water{
public static function drink(){
Zebra::action(__CLASS__);
}
}
Water::drink();
?>
Full usable solution using exception, but not debug_backtrace, no need to modify any prototype :
function getRealCallClass($functionName)
{
try
{
throw new exception();
}
catch(exception $e)
{
$trace = $e->getTrace();
$bInfunction = false;
foreach($trace as $trace_piece)
{
if ($trace_piece['function'] == $functionName)
{
if (!$bInfunction)
$bInfunction = true;
}
elseif($bInfunction) //found !!!
{
return $trace_piece['class'];
}
}
}
}
class Zebra{
public static function action(){
print 'I was called from the '.getRealCallClass(__FUNCTION__).' class';
}
}
class Water{
public static function drink(){
Zebra::action();
}
}
Water::drink();

How can I get a list of methods that are originally *defined* in a class in php?

I'm trying to get a list of methods that were actually defined in a definition for a given class (not just inherited from another class). For example:
class A
{ function bob()
{
}
}
class B extends A
{ function rainbrew()
{
}
}
class C extends B
{ function bob()
{
}
}
echo print_r(get_defined_class_methods("A"), true)."<br>\n";
echo print_r(get_defined_class_methods("B"), true)."<br>\n";
echo print_r(get_defined_class_methods("C"), true)."<br>\n";
I'd like the result to be:
array([0]=>bob)
array([0]=>rainbrew)
array([0]=>bob)
Is this possible?
You can use Reflection for this:
$reflectA = new ReflectionClass('A');
print_r($reflectA->getMethods());
This will actually give you an array of ReflectionMethod objects, but you can easily extrapolate what you need from there. ReflectionMethods provide a getDeclaringClass() function for this, so you can find which functions were declared in which class:
$reflectC = new ReflectionClass('C');
$methods = $reflectC->getMethods();
$classOnlyMethods = array();
foreach($methods as $m) {
if ($m->getDeclaringClass()->name == 'C') {
$classOnlyMethods[] = $m->name;
}
}
print_r($classOnlyMethods);
This will give:
Array ( [0] => bob )
So, as a final solution, try this:
function get_defined_class_methods($className)
{
$reflect = new ReflectionClass($className);
$methods = $reflect->getMethods();
$classOnlyMethods = array();
foreach($methods as $m) {
if ($m->getDeclaringClass()->name == $className) {
$classOnlyMethods[] = $m->name;
}
}
return $classOnlyMethods;
}
I haven't tried it, but it looks like you want to use get_class_methods
<?php
class myclass {
// constructor
function myclass()
{
return(true);
}
// method 1
function myfunc1()
{
return(true);
}
// method 2
function myfunc2()
{
return(true);
}
}
$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) {
echo "$method_name\n";
}
?>
The above example will output:
myclass myfunc1 myfunc2

PHP How to distinguish $this pointer in the inheritance chain?

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.

Categories