GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Guru -- here's a tough one! (https://gfy.com/showthread.php?t=986585)

qw12er 09-10-2010 10:39 PM

PHP Guru -- here's a tough one!
 
1) Why can't I use '$this' since when $func() is executed it will be in foo's object context ? (So $this should be available ?!)
2) How can I avoid this problem ? (cause I really need to call a function from class foo)


Quote:

class foo{

public function internal(){
print 'Hello world.';
}

public function __call($method, $args){
if(isset($this->$method)){
$func = $this->$method;
$func();
}
}
}

$f = new foo();
$f->bar = function () {$this->internal();};
$f->bar();

CS-Jay 09-10-2010 10:46 PM

it's late for me, but isn't it

foo::internal();

qw12er 09-10-2010 10:56 PM

foo::internal(); would work except for the fact that I really need $this because internal() normally use $this->properties...

I've oversimplified my example. Sorry.
This would be more accurate :

Quote:

class foo{
public $msg;

public function interne(){
print $msg;
}

public function __call($method, $args){
if(isset($this->$method)){
$func = $this->$method;
$func();
}
}
}

$f = new foo();
$f->msg = 'hello';
$f->bar = function () {foo::interne();};
$f->bar();
foo::interne(); won't 'know' that $f->msg has been set...

I also tryed call_user_func_array(array($f, 'bar'), array()); instead of $f->bar without success

Thanks for you help... specially at this hour.

Tempest 09-10-2010 11:59 PM

What version of PHP are you using?

qw12er 09-11-2010 12:01 AM

php 5.3.0

Tempest 09-11-2010 12:15 AM

I can't even do anonymous functions as none of my php versions are 5.3 or above.

Are you getting an error or is just not working? If error, what line is giving you the error?

qw12er 09-11-2010 07:19 AM

Ok it's possible to do this :

Quote:

$f->bar = function () use ($f) {$f->internal();};
but I need to dynamicaly build my function with create_function (since the body of my function is passed to me as a string) how do I work with the keyword "use" then ?

qw12er 09-11-2010 11:00 AM

bump 8char

mlove 09-11-2010 11:29 AM

I'm not a programmer, just a sysadmin, but perhaps it relates to register_globals being disabled?

borked 09-12-2010 01:31 AM

can't help really as I'm on 5.2.x, so all this isn't available, but according to http://php.net/manual/en/migration53.incompatible.php
Quote:

The call_user_func() family of functions now propagate $this even if the callee is a parent class.
goto http://php.net/manual/en/function.call-user-func.php
scroll down to user comment by
chris at NOSPAM dot panospheric dot com
23-Sep-2007 02:37

this looks like what you are trying to do?

hope it helps

qw12er 09-12-2010 08:45 AM

Quote:

Originally Posted by borked (Post 17494168)
can't help really as I'm on 5.2.x, so all this isn't available, but according to http://php.net/manual/en/migration53.incompatible.php


goto http://php.net/manual/en/function.call-user-func.php
scroll down to user comment by
chris at NOSPAM dot panospheric dot com
23-Sep-2007 02:37

this looks like what you are trying to do?

hope it helps

Close but it's not quite it. The problem am having is more when I declare the dynamic function then when I call it.

Ultimately what I would like to do is something like this :
Quote:

$code = 'print ($f->msg);';
$f->bar = function () use($f) { $code };
So the code of the function to be would be contain is $code as a string. But of course this returns an error...

Kiopa_Matt 09-12-2010 10:02 AM

Then use: $response = exec($code);

And what's the reason for you complicating shit beyond belief?

qw12er 09-12-2010 10:11 AM

Quote:

Originally Posted by RDFrame (Post 17495059)
Then use: $response = exec($code);

And what's the reason for you complicating shit beyond belief?


LOL it's complicated I'll give you that! but in my application users can inject their own code into my predefined objects. Lets say for overriding a behavior that doesn't fit their business rules ...

thanks for your help.

2012 09-12-2010 10:12 AM

that's a tuffy ... :xmas-smil22

qw12er 09-12-2010 10:51 AM

Here we go !
Thanks everyone for your help !

Code:

class foo{
  public $msg;

  public function __call($method, $args){
  if(isset($this->$method)){
      $func = $this->$method;
      call_user_func_array($func, $args);
    }
  }

  public function create(){
$method = 'bar';
$this->msg = 'hello';
$parameters = '$param1, $param2';
$param1 = 'William';
$param2 = 'Phorrend';
$code = 'print ($obj->msg." $param1 $param2");';
    $obj = $this;
    $fctStr = "return function($parameters) use(\$obj) { $code };";
   
    $this->$method = eval($fctStr);
    $this->$method($param1, $param2);
  }
}

$foo = new foo();
$foo->create();



All times are GMT -7. The time now is 06:40 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123