【问题标题】:Access a variable of a function of other class [duplicate]访问其他类的函数的变量[重复]
【发布时间】:2016-12-12 01:09:10
【问题描述】:

我有一个名为 VSE 的类,其中有超过 50 个函数。 在每个函数中我都有一个变量调用$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);
    }
    .
    .

}

我想列出所有这些$url 变量。

我已经

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

PHP 中是否有开箱即用的功能?

或者我必须构建一些 hacky 来获得它吗?

有什么提示吗?


@Machavity:

由于您已将我的帖子标记为重复。我试过你建议的链接:

我并没有真正看到我正在尝试访问的 $url 变量。

我错过了什么吗?

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
  ]
}

【问题讨论】:

  • 我不确定你的意思 - 因为它们是方法范围变量,你是否正在为你的类中的每个方法寻找 $url 的返回值?
  • 您是否尝试仅在测试环境中检索?
  • 我不确定要欺骗哪个,所以我选择了一个。另一个竞争者是stackoverflow.com/questions/11575724/…

标签: php laravel laravel-5


【解决方案1】:

听起来你想动态调用类方法。你也可以使用反射 API,语法见这里:

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

【讨论】:

  • 当一个问题是这样的重复时,请将其标记为关闭。重复的问题经常被删除
  • 会做的,谢谢你的建议。
  • @baku :实际上,您的建议不错。虽然,我有一个问题。
  • 这就是我返回的array:86 [▼ 0 =&gt; ReflectionMethod {#443 ▼ +name: "account" +class: "App\VSE" parameters: array:1 [▼ "$id" =&gt; ReflectionParameter {#537 ▼ +name: "id" position: 0 } ] modifiers: "public static" } 作为数组的第一个元素。我在这些课程中并没有真正看到我的 $url 变量......还有其他建议吗???
  • 所以,使用这个php.net/manual/en/reflectionclass.getmethods.php,来取回一个方法数组,然后你可以循环遍历取回的数组,像这样一个一个地执行它们; $this-&gt;{$methodName}($arg1, $arg2, $arg3); 或类似:call_user_func_array(array($this, $methodName), array($arg1, $arg2, $arg3)); 您将遍历每个方法,并且您的方法将单独返回 $url
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-02
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2018-10-16
相关资源
最近更新 更多