php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9054 problems in call_user_method
Submitted: 2001-02-01 12:17 UTC Modified: 2001-09-09 05:49 UTC
From: ken2000 at hudat dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.0.4pl1 OS: Linux/Solaris
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ken2000 at hudat dot com
New email:
PHP Version: OS:

 

 [2001-02-01 12:17 UTC] ken2000 at hudat dot com
call_user_method doesn't seem to allow the called method to modify it's own viriables and what not. It's as though it's copying an object instead of referencing the one specified. 

This is the same bug reported in Bug #6347. There weren't any additionally open bugs, but I'm using 4.0.4pl1 and I do not see a working solution yet. 

The original script follows. I modified it for a command line implementation (took out all the HTML):

----------- Example 1 ------------
class test
    {
        var $a=0;

        function increase()
        {
            $this->a++;
            echo "a is now: ".$this->a."\n";
        }

    }


    $t = new test();

    echo "Direct calls:\n";
    $t->increase();
    $t->increase();
    $t->increase();

    echo "\n\nIndirect calls:\n";

    call_user_method ("increase", $t);
    call_user_method ("increase", $t);
    call_user_method ("increase", $t);


this script should increment $t 6 times. It produces the following output:

Direct calls:
a is now: 1
a is now: 2
a is now: 3


Indirect calls:
a is now: 4
a is now: 4
a is now: 4



I also wrote a script to do something similar, and it follows. I call "call_user_method" both within the class (using $this) and outside the class.

----------------- Example 2 -------------
class MyClass {
   var $my_Var = "";

   function MyClass ($value = "") {
      $this->my_Var = $value;
      print("my_Var set to " . $this->my_Var . "\n");
   }

   function change ($value = "") {
      print("changing to " . $value . "\n");
      call_user_method("MyClass", &$this, "2");
   }

   function show () {
      print("my_Var is " . $this->my_Var . "\n");
   }
}

$mine = new MyClass("1");
$mine->show();
$mine->change("2");
$mine->show();
call_user_method("MyClass", $mine, "3");
$mine->show();

This should produce the following output:

my_Var set to 1
my_Var is 1
changing to 2
my_Var set to 2
my_Var is 2
my_Var set to 3
my_Var is 3


However it produces this:

my_Var set to 1
my_Var is 1
changing to 2
my_Var set to 2
my_Var is 1
my_Var set to 3
my_Var is 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-09-09 05:49 UTC] sterling@php.net
you must pass a reference, anyhoo, call_user_method() is
deprecated, use call_user_function(array(&$obj, "Meethod"));
syntax...
 [2001-09-09 05:49 UTC] sterling@php.net
forgot to bogify
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 03 14:01:29 2024 UTC