php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #24105 class member functions aren't first class data objects
Submitted: 2003-06-09 19:32 UTC Modified: 2003-06-28 09:56 UTC
From: knotwell at ix dot netcom dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.1 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: knotwell at ix dot netcom dot com
New email:
PHP Version: OS:

 

 [2003-06-09 19:32 UTC] knotwell at ix dot netcom dot com
I've checked the documentation, but it doesn't address this
issue.  As a result, I'm unsure if this is a bug or by design.

Anyhow, it appears class functions aren't first class data objects.  I've included a short example leading to a
"Call to undefined function" message as a example:

<?php

function generic_data_handler($specializedFn,$specializedFnData) {
    return $specializedFn($specializedFnData);
}

class z {
   var $x = 10;
   var $y = 4;

   function _mult($me) {
       return($me->x * $me->y);
   }

   function aStupidlyContrivedExample() {
       return generic_data_handler($this->_mult,$this); 
   }
}

$a = new z;
print $a->_mult($a);             

// an error from the runtime system
print $a->aStupidlyContrivedExample();   

?>

Apologies in advance if this is common knowledge or not a bug.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-06-09 23:28 UTC] sniper@php.net
It's by design.
You're supposed to pass a string there, see below:

<?php

function generic_data_handler($specializedFn,$specializedFnData) {
    return $specializedFnData->$specializedFn($specializedFnData);
}
 
class z {
   var $x = 10;
   var $y = 4;

   function _mult($me) {
       return($me->x * $me->y);
   }
    
   function aStupidlyContrivedExample() {
       return generic_data_handler('_mult', $this);
   }
}
 
$a = new z;
print $a->_mult($a);

// an error from the runtime system
print $a->aStupidlyContrivedExample();

?>

 [2003-06-10 00:42 UTC] knotwell at ix dot netcom dot com
So the methodology for this is (what I presume to be)introspection, if possible, I'd suggest the addition to the documentation address the following similar case as well:

class z {
   function b($x,$y) {
       return $x*$y;
   }
}

$a = new z;
$b = $a->b;
$b(3,4) --> an error from the runtime system.
 [2003-06-28 09:56 UTC] helly@php.net
No, you're not requesting introspection here (btw. we will have that in php5). What you want are function pointers. and your second example is only the problem rewritten. Look at sniper's mail for the solution. If you want to pass object and method the notation is array($obj,$methodname).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 17 14:01:27 2024 UTC