note 57842 added to function.get-class-methods

From: Date: Sun, 16 Oct 2005 13:37:34 +0000
Subject: note 57842 added to function.get-class-methods
Groups: php.notes 
Request: Send a blank email to php-notes+get-97082@lists.php.net to get a copy of this message
In PHP4, this function converts its return values to lowercase; but in PHP5, it leaves the return values in their original case. This can cause serious problems when trying to write code that dynamically calls a class method, and that works in both PHP4 and PHP5. This code snippet shows one way of achieving compatibility with both versions: <?php // Example variables - these would be dynamic in a real application. $className = 'SomeClass'; $methodName= 'someMethod'; $args = array('arg1', 'arg2'); // Use array_map() and strtolower() to make all values returned by get_class_methods() lowercase. PHP4 does this anyway - now it will happen regardless of the PHP version being used. $classMethods = array_map(strtolower, get_class_methods($className)); // in_array() can only handle being given an array. if (!$classMethods) { $classMethods = array(); } if (in_array(strtolower($methodName), $classMethods)) { // call some method return call_user_func_array(array($className, $methodName), $args); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.get-class-methods.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+57842 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57842&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+57842&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97082) next »