Web Analytics Made Easy -
StatCounter &function (Need further explaination) - CodingForum

Announcement

Collapse
No announcement yet.

&function (Need further explaination)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • &function (Need further explaination)

    What does this mean:
    PHP Code:
    function &function_name(..) {


    (Note the & in front of function_name)

    How can it even be used/useful?

  • #2
    It returns a reference to a value.
    Lesee... I'll go with an object though you can use procedural for the same effect. This is just easier to show.
    PHP Code:
    class MyClass
    {
        private 
    $value;
        public function 
    __construct($value)
        {
            
    $this->value $value;
        }
        public function &
    getValue()
        {
            return 
    $this->value;
        }
        public function 
    setValue($value)
        {
            
    $this->value $value;
        }
    }

    $obj = new MyClass('Hello World');
    $value = &$obj->getValue();
    $obj->setValue('This is a test');
    print 
    $value// Results in: This is a test 
    The only practical uses I've found for using returned references is when I'm implementing templating systems that make use of counting queries executed and runtimes. Since the template is queried, you can't actually include the correct query count without using a referenced variable (otherwise you'd need to query again and again and again and so forth to never get a correct count).
    Make sense? func_get_args() for example does not return a reference. This is a pain since you actually need to assign a func_get_args() to a variable before operating on it, for example passing it to a call_user_func.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

    Comment


    • #3
      Thanks Fou-Lu. Can you show an example of how this will be used outside of objects, though? But that object does explain a lot.

      Comment


      • #4
        Yeah, it doesn't really have a lot of purpose to be quite honest.
        PHP Code:
        function &countme()
        {
            static 
        $i 0;
            return ++
        $i;
        }

        $v1 = &countme(); // Ok, $v1 = 1
        countme();
        countme();
        countme(); // Count me has incremented its $i to 4
        print $v1// Should now print 4 
        Note as well that you're using the & in front of both the function and the reference for the function call. This is required to use the return by reference technique. Otherwise, you'll be presented with a non-changing value, since it will copy the returned reference into the variable.
        PHP Code:
        header('HTTP/1.1 420 Enhance Your Calm'); 
        Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

        Comment

        Working...
        X