0

I have a class that generates data based on a few things. I would like to format that data from the outside. So I am trying to pass a function into the class so that it would format that data. I have looked at many examples, but it seems this is unique.

Can anybody give an idea of how to do this? The following code gives an error.

<?php
class someClass { 
    var $outsideFunc; // placeholder for function to be defined from outside
    var $somevar='Me'; // generated text 
    function echoarg($abc){ 
        $outsideFunc=$this->outsideFunc; // bring the outside function in
        call_user_func($outsideFunc,$abc); // execute outside function on text
        echo $abc;
    }

}

function outsidefunc($param){ // define custom function
    $param='I am '.$param;
}

$someClass=new someClass();
$someClass -> outsideFunc = 'outsideFunc'; // send custom function into Class
$someClass -> echoarg($someClass->somevar);
user139301
  • 344
  • 6
  • 12
  • Structurally, you may be better off passing an entire object into your method. Then that method can call the object methods it needs to accomplish what it needs.. it's called the Decorator Pattern and covered well here: http://stackoverflow.com/questions/3235382/help-me-to-avoid-multiple-inheritance-aka-help-me-to-get-proper-oo-design – CaseySoftware Dec 30 '12 at 08:05

4 Answers4

1

Why not pass your function as an argument?

<?php
class someClass {
  public $somevar="Me";
  public function echoarg($abc,$cb=null) {
    if( $cb) $cb($abc);
    echo $abc;
  }
}

$someClass = new someClass();
$someClass->echoarg($someClass->somevar,function(&$a) {$a = "I am ".$a;});
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1
$someClass -> outsidefunc = 'outsidefunc';

In PHP, function names are not case sensitive, yet object property names are. You need $someClass->outsideFunc, not $someClass->outsidefunc.

Note that good OOP design practice calls for the use of getter and setter methods rather than just accessing properties directly from outside code. Also note that PHP 5.3 introduced support for anonymous functions.


Yeah. You are right. Now there is no error. But it does not work either.

By default, PHP does not pass arguments by reference; outsidefunc() does not actually do anything useful. If you want it to set $param in the caller to something else, and do not want to just return the new value, you could change the function signature to look like this:

function outsidefunc(&$param) {

You would also need to change the way you call the function, as call_user_func() does not allow you to pass arguments by reference. Either of these ways should work:

$outsideFunc($abc);
call_user_func_array($outsideFunc, array(&$abc));
Community
  • 1
  • 1
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
0

i am not sure what exactly you are looking for, but what i get is, you want to pass object in a function which can be acheive by Type Hinting in PHP.

class MyClass {
    public $var = 'Hello World';
}

function myFunction(MyClass $foo) {
    echo $foo->var;
}
$myclass = new MyClass;
myFunction($myclass);
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • Yeah. TYpe Hinting should do the trick, but for functions type hinting is 'callable' PHP 5.3 does not support callable type. – user139301 Dec 30 '12 at 07:32
0

OP, perhaps closures are what you're looking for? It doesn't do EXACTLY what you're looking for (actually add function to class), but can be added to a class variable and executed like any normal anonymous function.

$myClass->addFunc(function($arg) { return 'test: ' . $arg });
$myClass->execFunc(0);

class myClass {
    protected $funcs;
    public function addFunc(closure $func) {
        $this->funcs[] = $func;
    }

    public function execFunc($index) { $this->funcs[$index](); } // obviously, do some checking here first.
}
David Harris
  • 2,697
  • 15
  • 27