By matt richards

2018-09-14 10:24:31 8 Comments

I am using the stdObject methodology in my program, and essentially wondering if it is possible to return a reference :

//standard way using classes


class test {
  function & gettxt(){
    global $txt;
    return $txt;
  function disp(){
    global $txt;
    echo $txt;
$o=new test();
$txt_ref=& $o->gettxt();
$o->disp();//displays world : anonymous functions

sugguests this syntax :


class stdObject {
  public function __call($method,$arguments){
      return call_user_func_array($this->{$method},$arguments);
      throw new Exception("Fatal error: Call to undefined method, $method");


$o=new stdObject();
$o->getvalue=function & () use (&$txt) { return $txt;};
$o->disp=function() use (&$txt) { echo $txt;};

$txt_ref=& $o->getvalue();//error only variables should be assigned by reference
$o->disp();//hoping for 'world'


@matt richards 2018-09-14 15:57:30

class stdobject {

  function & __call($method,$args){

      $r=& ($this->{$method})();
    return $r;




$o=new stdobject();
$o->getvalue_=function & () use (&$txt) { return $txt;};
$o->disp=function () use (&$txt) { echo $txt; };

$txt_ref=& $o->getvalue_();
$o->disp();  // world

well i settled on this solution

  • php7
  • it uses a programming convention that functions that return a reference should be declared with their names ending in an underscore
  • this example does not provide arguments to the function, but can be incorporated using a variety of methods
  • php5 requires functions returning references to be written as

    $r=& $fn();

@sg- 2018-09-14 12:58:41

What you are proposing is a bad idea as it breaks the rule of Encapsulation in Object Oriented programming because you are trying to gain a direct reference to an object's internal property, and that is generally prohibited in OO.

The correct way to achieve it is by adding a setter method:

class test {
    public setValue($val)
       $this->value = $val;

However, if you insist on breaking the rules of OO programming you can do it this way by making the object's internal property public:

class test {
    public $value;

$o=new test();
$o->value = 'world';

@matt richards 2018-09-14 13:05:54

ive been trying to simplify the working code useage, i am using the stdObject methodology for a variety of reasons, non polution of global splace, the way i am storing and loading code also, ive essentially solved the working code problem by returning an array index rather than a reference to an array, so am asking the question mostly for academic reasons

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

15 Answered Questions

[SOLVED] NOW() function in PHP

30 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 771719 View
  • 1349 Score
  • 30 Answer
  • Tags:   php string

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

16 Answered Questions

[SOLVED] Returning JSON from a PHP Script

  • 2010-10-31 18:23:54
  • Scott Nicol
  • 874388 View
  • 758 Score
  • 16 Answer
  • Tags:   php json header

15 Answered Questions

[SOLVED] Why shouldn't I use mysql_* functions in PHP?

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 203205 View
  • 2376 Score
  • 15 Answer
  • Tags:   php mysql database

9 Answered Questions

5 Answered Questions

[SOLVED] PHP __call vs method_exists

  • 2009-10-01 18:37:43
  • neo
  • 8989 View
  • 14 Score
  • 5 Answer
  • Tags:   php overloading

1 Answered Questions

[SOLVED] PHP implements ArrayAccess

1 Answered Questions

PHP constructor return object with pass by reference

Sponsored Content