メソッド名一覧の表示
Posted feedbacks - PHP
get_class_methods関数を使うと簡単にメソッド一覧を取得できます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php
class Doukaku
{
function Doukaku()
{
}
function test_Doukaku()
{
}
function hello()
{
print("hello world!");
}
function test_hello()
{
}
}
$doukaku = new Doukaku();
$result = array();
foreach (get_class_methods($doukaku) as $method) {
if (strpos($method, "test_") === 0) {
$result[] = $method;
}
}
var_dump($result);
?>
|
実行するとこが抜けてたorz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php
class Doukaku
{
function php()
{
print("php\n");
}
function test_php()
{
print("test_php\n");
}
function hello()
{
print("hello\n");
}
function test_hello()
{
print("test_hello\n");
}
}
$doukaku = new Doukaku();
$result = array();
foreach (get_class_methods($doukaku) as $method) {
if (strpos($method, "test_") === 0) {
$doukaku->$method();
}
}
?>
|
環境選びますがfnmatchとかいかがざんすか?
1 2 3 4 5 | foreach (get_class_methods($doukaku) as $method) {
if (fnmatch("test_*", $method)) {
$doukaku->$method();
}
}
|



にしお
#3388()
Rating1/1=1.00
「ある与えられたオブジェクトtargetのメソッドのうち、 "test_"で始まるものをすべて呼びだす」というコードを書いてください。 引数に関しては都合のいいように仮定して構いません(全部0個、など)。
メソッドという概念がない言語の場合は、 「複数の関数への参照を持っているようなオブジェクト(たとえばパッケージとかモジュールとか)から"test_"で始まる関数をすべて呼び出す」と読み替えても構いません。
[ reply ]