0

I have a class call VSE, and I have more than 50 functions in there. In every function I have a variable call $url

<?php
namespace App;
use Request, Cache, App\Helper;


class VSE {

    .
    .
     public static function wifi_device_state_all($cpe_mac, $wifi_index) {
        $url = env('API_HOST').'vse/vcpe/'.$cpe_mac.'/wifi_index/'.$wifi_index.'/wifi_device_state.all';
        return Helper::getData($url);
    }
    .
    .

}

I want to list all of those $url variables.

I've

$functions = get_class_methods(\App\VSE::class);
    foreach ($functions as $function) {
        dd($function); // I got function name printed
    }

Is there a function in PHP is offer something like that right out of the box ?

Or do I have to build something hacky to get that ?

Any hints on that ?


@Machavity :

Since you've mark my posted as duplicate. I've tried the link you suggested :

I didn't really see my $url variable that I am trying to access.

Am I missing something here ?

ReflectionClass {#444 ▼
  +name: "App\VSE"
  implements: []
  constants: []
  methods: array:86 [▼
    "account" => ReflectionMethod {#452 ▼
      +name: "account"
      +class: "App\VSE"
      parameters: array:1 [▼
        "$id" => ReflectionParameter {#542 ▼
          +name: "id"
          position: 0
        }
      ]
      modifiers: "public static"
    }
    "account_all" => ReflectionMethod {#453 ▼
      +name: "account_all"
      +class: "App\VSE"
      modifiers: "public static"
    }
    "count_accounts" => ReflectionMethod {#454 ▼
      +name: "count_accounts"
      +class: "App\VSE"
      modifiers: "public static"
    }
    "locations" => ReflectionMethod {#455 ▼
      +name: "locations"
      +class: "App\VSE"
      modifiers: "public static"
    }
    "location" => ReflectionMethod {#456 ▼
      +name: "location"
      +class: "App\VSE"
      parameters: array:1 [▼
        "$id" => ReflectionParameter {#547 ▼
          +name: "id"
          position: 0
        }
      ]
      modifiers: "public static"
    }
    "follow_me" => ReflectionMethod {#457 ▼
      +name: "follow_me"
      +class: "App\VSE"
      parameters: array:1 [▼
        "$id" => ReflectionParameter {#549 ▼
          +name: "id"
          position: 0
        }
      ]
      modifiers: "public static"
    }
    "vcpe" => ReflectionMethod {#458 ▶}
    "cpe" => ReflectionMethod {#459 ▶}
    "vcpes_all" => ReflectionMethod {#460 ▶}
    "vcpes_in_account" => ReflectionMethod {#461 ▶}
    "cpe_name" => ReflectionMethod {#462 ▶}
    "ipv6_portforward" => ReflectionMethod {#463 ▶}
    "ipv6_enable" => ReflectionMethod {#464 ▶}
    "ipv4_block_fragment_packets" => ReflectionMethod {#465 ▶}
    "ipv6_block_fragment_packets" => ReflectionMethod {#466 ▶}
    "ipv6_firewall" => ReflectionMethod {#467 ▶}
    "acl" => ReflectionMethod {#468 ▶}
    "tunnel_states" => ReflectionMethod {#469 ▶}
    "tunnel_state" => ReflectionMethod {#470 ▶}
    "cgnat" => ReflectionMethod {#471 ▶}
    "cpe_location" => ReflectionMethod {#472 ▶}
    "vcpes_account_count" => ReflectionMethod {#473 ▶}
    "vcpes_count" => ReflectionMethod {#474 ▶}
    "count_tunnel_states" => ReflectionMethod {#475 ▶}
    "count_tunnel_state" => ReflectionMethod {#476 ▶}
    "count_device_states" => ReflectionMethod {#477 ▶}
    "count_device_state" => ReflectionMethod {#478 ▶}
    "count_device" => ReflectionMethod {#479 ▶}
    "private_devices" => ReflectionMethod {#480 ▶}
    "guest_devices" => ReflectionMethod {#481 ▶}
    "device_state" => ReflectionMethod {#482 ▶}
    "private_groups" => ReflectionMethod {#483 ▶}
    "group_devices" => ReflectionMethod {#484 ▶}
    "private_bandwidth" => ReflectionMethod {#485 ▶}
    "this_bandwidth" => ReflectionMethod {#486 ▶}
    "guest_bandwidth" => ReflectionMethod {#487 ▶}
    "total_bandwidth" => ReflectionMethod {#488 ▶}
    "device" => ReflectionMethod {#489 ▶}
    "device_rate_limit" => ReflectionMethod {#490 ▶}
    "device_acl_all" => ReflectionMethod {#491 ▶}
    "device_acl" => ReflectionMethod {#492 ▶}
    "schedule_use_data" => ReflectionMethod {#493 ▶}
    "schedule_use" => ReflectionMethod {#494 ▶}
    "bandwidth" => ReflectionMethod {#495 ▶}
    "wan" => ReflectionMethod {#496 ▶}
    "wan_bandwidth" => ReflectionMethod {#497 ▶}
    "wan_vlan_bandwidth" => ReflectionMethod {#498 ▶}
    "wan_bandwidth2" => ReflectionMethod {#499 ▶}
    "portforward" => ReflectionMethod {#500 ▶}
    "cpe_lan" => ReflectionMethod {#501 ▶}
    "lan" => ReflectionMethod {#502 ▶}
    "this_lan_acl" => ReflectionMethod {#503 ▶}
    "lan_acl" => ReflectionMethod {#504 ▶}
    "this_lan_acl_device" => ReflectionMethod {#505 ▶}
    "lan_acl_device" => ReflectionMethod {#506 ▶}
    "dnsbridge" => ReflectionMethod {#507 ▶}
    "device_dnsbridge" => ReflectionMethod {#508 ▶}
    "p_dnsbridge" => ReflectionMethod {#509 ▶}
    "g_dnsbridge" => ReflectionMethod {#510 ▶}
    "security_level_vlan" => ReflectionMethod {#511 ▶}
    "security_level_device" => ReflectionMethod {#512 ▶}
    "security_level_group" => ReflectionMethod {#513 ▶}
    "this_acl" => ReflectionMethod {#514 ▶}
    "guest_acl" => ReflectionMethod {#515 ▶}
    "this_devicebandwidth" => ReflectionMethod {#516 ▶}
    "devicebandwidth" => ReflectionMethod {#517 ▶}
    "this_network_bandwidth" => ReflectionMethod {#518 ▶}
    "guest_network_bandwidth" => ReflectionMethod {#519 ▶}
    "this_lan" => ReflectionMethod {#520 ▶}
    "lan_guest" => ReflectionMethod {#521 ▶}
    "hour_vlan" => ReflectionMethod {#522 ▶}
    "week_vlan" => ReflectionMethod {#523 ▶}
    "month_vlan" => ReflectionMethod {#524 ▶}
    "device_hour" => ReflectionMethod {#525 ▶}
    "device_week" => ReflectionMethod {#526 ▶}
    "device_month" => ReflectionMethod {#527 ▶}
    "wifi_device_state" => ReflectionMethod {#528 ▶}
    "wifi_device_passphrase" => ReflectionMethod {#529 ▶}
    "wifi_device_ssid" => ReflectionMethod {#530 ▶}
    "wifi_device_list" => ReflectionMethod {#531 ▶}
    "wifi_device_state_all" => ReflectionMethod {#532 ▶}
    "cloud_security" => ReflectionMethod {#533 ▶}
    "mirror_server" => ReflectionMethod {#534 ▶}
    "facebook" => ReflectionMethod {#535 ▶}
    "volume_limit" => ReflectionMethod {#536 ▶}
    "volume_limit_current" => ReflectionMethod {#537 ▶}
  ]
  extra: array:4 [▼
    "file" => "/Applications/MAMP/htdocs/code/site/portal/app/VSE.php"
    "line" => "14 to 609"
    "docComment" => """
      /**\n
       * Test Default Value\n
       * env('API_HOST') = 172.16.200.204\n
       * $cpe_mac = 000D6751560C\n
       * $account_id = 1002\n
       *\n
       */
      """
    "isUserDefined" => true
  ]
}
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    I'm not sure what you mean - since they're method scope variables, are you looking for the returned value of `$url` for every method in your class? – scrowler Dec 12 '16 at 01:28
  • Are you trying to retrieve in a test environment only? – Shane Dec 12 '16 at 01:28
  • I wasn't sure which to dupe this as so I picked one. The other contender was http://stackoverflow.com/questions/11575724/php-how-to-list-out-all-public-functions-of-class – Machavity Dec 12 '16 at 01:30

1 Answers1

0

Sounds like you want to call the class method dynamically. You can use also use the reflection API, see here for syntax:

https://stackoverflow.com/a/251512/7281447

Community
  • 1
  • 1
baku
  • 765
  • 8
  • 22
  • When a question is a duplicate like this, please flag it for closure. Duplicate questions are often deleted – Machavity Dec 12 '16 at 01:33
  • will do, thank you for the advice. – baku Dec 12 '16 at 02:40
  • @baku : Actually, your suggestion is kind of good. Although, I have a question. – code-8 Dec 12 '16 at 13:54
  • This is what I got back `array:86 [▼ 0 => ReflectionMethod {#443 ▼ +name: "account" +class: "App\VSE" parameters: array:1 [▼ "$id" => ReflectionParameter {#537 ▼ +name: "id" position: 0 } ] modifiers: "public static" }` as for my first element of the array. I didn't really see my $url variable in those class ... any other suggestion ??? – code-8 Dec 12 '16 at 13:54
  • so, use this http://php.net/manual/en/reflectionclass.getmethods.php, to get back an array of methods, then you can loop throughtthe array you get back, executing them one by one like so; `$this->{$methodName}($arg1, $arg2, $arg3);` or like so: `call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3));` you will iterate over each one of your methods, and your methods will return the $url individually – baku Dec 12 '16 at 15:45