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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 19 02:01:29 2024 UTC