★ wanayoo — archive 1999 http://fr.php.net/manual/de/function.get-class-methods.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<class_existsget_class_vars>
view the version of this page
Last updated: Fri, 01 Apr 2005

get_class_methods

(PHP 4 , PHP 5)

get_class_methods --  Liefert die Namen aller Methoden einer Klasse

Beschreibung

array get_class_methods ( string class_name )

Diese Funktion liefert ein String-Array zurück, das mit den Namen aller in der Klasse class_name definerten Methoden gefüllt ist.



add a note add a note User Contributed Notes
get_class_methods
Oli Filth
11-Apr-2005 05:21
As an extension to onesimus's code below for finding inherited methods, in PHP 5 you can use the Reflection API to find which of these are overriden.

e.g.

<?php
function get_overriden_methods($class)
{
  
$rClass = new ReflectionClass($class);
  
$array = NULL;
      
   foreach (
$rClass->getMethods() as $rMethod)
   {
      
try
      
{
          
// attempt to find method in parent class
          
new ReflectionMethod($rClass->getParentClass()->getName(),
                              
$rMethod->getName());
          
// check whether method is explicitly defined in this class
          
if ($rMethod->getDeclaringClass()->getName()
               ==
$rClass->getName())
           {
              
// if so, then it is overriden, so add to array
              
$array[] .=  $rMethod->getName();
           }
       }
      
catch (exception $e)
       {   
/* was not in parent class! */    }
   }
  
   return
$array;
}
?>
rdude at fuzzelfish dot com
23-Mar-2005 06:42
In PHP5, you cannot use get_class_methods by itself if you are trying to figure out what functions you can call from a certain class. This is because get_class_methods returns an array containing all methods of the class, even those that are private and protected.
kabatak
20-Feb-2005 02:11
In PHP4, if you need to get_class_methods in their original case. You can use this simple function I created.

// Note: this function assumes that you only have 1 class in 1 file

$file = "path/to/myclass.php"

function file_get_class_methods ($file)
{
   $arr = file($file);
   foreach ($arr as $line)
   {
       if (ereg ('function ([_A-Za-z0-9]+)', $line, $regs))
           $arr_methods[] = $regs[1];
   }
   return $arr_methods;
}
r0bin at r0bin dot net
18-Feb-2005 03:21
You cant get the Methods of COM components. So trying to get the methods from an instance of PHP's COM class will fail.
epowell at removethis dot visi dot com
11-Oct-2004 05:47
I've figured out how to get around my issue described below, using the Reflection API.

<?
/* Pass the name of the class, not a declared handler */
function get_public_methods($className) {
  
/* Init the return array */
  
$returnArray = array();

  
/* Iterate through each method in the class */
  
foreach (get_class_methods($className) as $method) {

      
/* Get a reflection object for the class method */
      
$reflect = new ReflectionMethod($className, $method);

      
/* For private, use isPrivate().  For protected, use isProtected() */
       /* See the Reflection API documentation for more definitions */
      
if($reflect->isPublic()) {
          
/* The method is one we're looking for, push it onto the return array */
          
array_push($returnArray,$method);
       }
   }

  
/* return the array to the caller */
  
return $returnArray;
}
?>
onesimus at cox dot net
19-Jun-2004 11:32
This function will return only the methods for the object you indicate. It will strip out the inherited methods.

function get_this_class_methods($class){
   $array1 = get_class_methods($class);
   if($parent_class = get_parent_class($class)){
       $array2 = get_class_methods($parent_class);
       $array3 = array_diff($array1, $array2);
   }else{
       $array3 = $array1;
   }
   return($array3);
}
gk at proliberty dot com
02-Jun-2003 01:16
It is important to note that get_class_methods($class) returns not only methods defined by $class but also the inherited methods.

There does not appear to be any PHP function to determine which methods are inherited and which are defined explicitly by a class.
aldo at cerca dot com
22-Apr-2002 07:18
If you use "get_class_methods" to check if a Method is in a Class remember that the function return lowered name of class methods:

class classPippo
{
       function DummyFunct()
       {
               // Do nothing...
       }
}

$aClassMethods = get_class_methods(classPippo);

$sMethodName = 'DummyFunct';

// This not work...

       if (in_array($sMethodName, $aClassMethods))
       classPippo::DummyFunct();

// This work...

       if (in_array(strtolower($sMethodName), $aClassMethods))
       classPippo::DummyFunct();
matt at zevi dot net
22-Mar-2002 01:46
Win32 only:

It's probably worth noting here that you can't get the methods of an object created by the built-in 'COM' class. ie - this won't work:

$word = new COM('Word.Application');
$methods = get_class_methods(get_class($word));
print_r($methods);

Matt

<class_existsget_class_vars>
 Last updated: Fri, 01 Apr 2005
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Mon Apr 18 05:18:33 2005 CEST