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

$txt='hello';

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

php.net : anonymous functions

sugguests this syntax :

//codefragment1:

//from php.net
class stdObject {
  public function __call($method,$arguments){
    if(isset($this->{$method})&&(is_callable($this->{$method}))
      return call_user_func_array($this->{$method},$arguments);
    else
      throw new Exception("Fatal error: Call to undefined method, $method");
  }
}

$txt='hello';

$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
$txt_ref='world';
$o->disp();//hoping for 'world'

2 comments

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

class stdobject {

  function & __call($method,$args){

    if(substr($method,-1)==='_')
      $r=& ($this->{$method})();
    else
      $r=($this->{$method})();
    return $r;

  }

}

$txt='hello';

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

$txt_ref=& $o->getvalue_();
$txt_ref='world';
$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

    $fn=$o->getvalue_;
    $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

14 Answered Questions

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

  • 2012-10-12 13:18:39
  • Madara Uchiha
  • 188978 View
  • 2264 Score
  • 14 Answer
  • Tags:   php mysql database

17 Answered Questions

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

30 Answered Questions

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

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

31 Answered Questions

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

15 Answered Questions

[SOLVED] Returning JSON from a PHP Script

  • 2010-10-31 18:23:54
  • Scott Nicol
  • 768634 View
  • 672 Score
  • 15 Answer
  • Tags:   php json header

9 Answered Questions

2 Answered Questions

[SOLVED] php call to undefined function from within a class

  • 2015-11-14 11:34:49
  • Jeff
  • 526 View
  • 1 Score
  • 2 Answer
  • Tags:   php

5 Answered Questions

[SOLVED] PHP __call vs method_exists

  • 2009-10-01 18:37:43
  • neo
  • 8523 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