Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 09-10-2010, 10:39 PM   #1
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
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();
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-10-2010, 10:46 PM   #2
CS-Jay
Confirmed User
 
CS-Jay's Avatar
 
Join Date: Oct 2003
Location: Command Central, West Palm Beach, Fl
Posts: 1,794
it's late for me, but isn't it

foo::internal();
__________________
I do stuff - aIm CS_Jay_D
CS-Jay is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-10-2010, 10:56 PM   #3
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
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.
__________________
I have nothing to advertise ... yet.

Last edited by qw12er; 09-10-2010 at 11:05 PM..
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-10-2010, 11:59 PM   #4
Tempest
Too lazy to set a custom title
 
Industry Role:
Join Date: May 2004
Location: West Coast, Canada.
Posts: 10,217
What version of PHP are you using?
Tempest is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-11-2010, 12:01 AM   #5
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
php 5.3.0
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-11-2010, 12:15 AM   #6
Tempest
Too lazy to set a custom title
 
Industry Role:
Join Date: May 2004
Location: West Coast, Canada.
Posts: 10,217
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?
Tempest is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-11-2010, 07:19 AM   #7
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
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 ?
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-11-2010, 11:00 AM   #8
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
bump 8char
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-11-2010, 11:29 AM   #9
mlove
the guy
 
mlove's Avatar
 
Industry Role:
Join Date: Apr 2005
Posts: 764
I'm not a programmer, just a sysadmin, but perhaps it relates to register_globals being disabled?
__________________
If you won't feel as good, I won't feel as cheap.
mlove is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 01:31 AM   #10
borked
Totally Borked
 
borked's Avatar
 
Industry Role:
Join Date: Feb 2005
Posts: 6,284
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
__________________

For coding work - hit me up on andy // borkedcoder // com
(consider figuring out the email as test #1)



All models are wrong, but some are useful. George E.P. Box. p202
borked is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 08:45 AM   #11
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
Quote:
Originally Posted by borked View Post
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...
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 10:02 AM   #12
Kiopa_Matt
Confirmed User
 
Industry Role:
Join Date: Oct 2007
Posts: 1,448
Then use: $response = exec($code);

And what's the reason for you complicating shit beyond belief?
__________________
xMarkPro -- Ultimate Blog Network Management
Streamline your marketing operations. Centralize management of domains, pages, Wordpress blogs, sponsors, link codes, media items, sales and traffic statistics, plus more!

Last edited by Kiopa_Matt; 09-12-2010 at 10:04 AM..
Kiopa_Matt is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 10:11 AM   #13
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
Quote:
Originally Posted by RDFrame View Post
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.
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 10:12 AM   #14
2012
So Fucking What
 
2012's Avatar
 
Industry Role:
Join Date: Jul 2006
Posts: 17,189
that's a tuffy ...
__________________
best host: Webair | best sponsor: Kink | best coder: 688218966 | Go Fuck Yourself
2012 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-12-2010, 10:51 AM   #15
qw12er
Confirmed User
 
Join Date: Apr 2004
Location: Montreal
Posts: 799
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();
__________________
I have nothing to advertise ... yet.
qw12er is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.