0

Can you pass in a $this variable to use in a function in the "global" space like you can in javascript? I know $this is meant for classes, but just wondering. I'm trying to avoid using the "global" keyword.

For example:

class Example{
  function __construct(){ }
  function test(){ echo 'something'; }
}

function outside(){ var_dump($this); $this->test(); }

$example = new Example();

call_user_func($example, 'outside', array('of parameters')); //Where I'm passing in an object to be used as $this for the function

In javascript I can use the call method and assign a this variable to be used for a function. Was just curious if the same sort of thing can be accomplished with PHP.

Senica Gonzalez
  • 7,996
  • 16
  • 66
  • 108

2 Answers2

3

PHP is very much different from JavaScript. JS is a prototype based language whereas PHP is an object oriented one. Injecting a different $this in a class method doesn't make sense in PHP.

What you may be looking for is injecting a different $this into a closure (anonymous function). This will be possible using the upcoming PHP version 5.4. See the object extension RFC.

(By the way you can indeed inject a $this into a class which is not instanceof self. But as I already said, this doesn't make no sense at all.)

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • Thanks for the link for the RFC. – Senica Gonzalez Oct 14 '11 at 23:22
  • X-Ref: http://stackoverflow.com/questions/3943486/restrict-what-can-create-a-php-class/3944238#3944238 – hakre Oct 14 '11 at 23:27
  • As a very late "just to clear things up" comment: Both PHP and JS are fully object oriented. The difference lies in the *type* of object system each one uses. PHP uses a *class based* object system (as many other languages do), JavaScript uses a *prototype based* object system (which could emulate a class based one if you really want it to). But both are fully fledged object oriented languages. – Timusan Mar 23 '15 at 23:33
0

Normally, you would just pass it as a reference:

class Example{
  function __construct(){ }
  function test(){ echo 'something'; }
}

function outside(&$obj){ var_dump($obj); $obj->test(); }

$example = new Example();

call_user_func_array('outside', array(&$example));

It would kind of defeat the purpose of private and protected vars, to be able to access "$this" of an obj from outside of the code. "$this", "self", and "parent" are keywords exclusive to specific objects that they're being used in.

Shea
  • 1,965
  • 2
  • 18
  • 42
  • Why as reference? I would normally pass it just as an object identifier *value*. – hakre Oct 14 '11 at 23:16
  • Why are you duplicating answers? – Senica Gonzalez Oct 14 '11 at 23:18
  • This is actually a different answer. Notice the shiny-fancy addition of references? You can also create a static registry class, which would hold all of your objects, which is the direction I would take. I believe newer versions of PHP automatically passes references for cases like these, but it's best to force them imo, for older versions. Iirc, you would want to make it a reference so that your class isn't read only, and also for memory management purposes. – Shea Oct 14 '11 at 23:31