- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在整合 Stripe API使用 CMS。我需要从查询中返回 _values
属性作为数组,以便数据在 CMS 中作为模板变量可用,但它始终受到保护。
我一直在使用 Reflection class获取数据,但现在我正在使用 Stripe 的 \Stripe\Plan::all();
,我不得不多次调用我写的快捷方法来处理Reflection类。不过,它并不完全是递归的,因为我必须根据从 Stripe API 调用的方法来不同地处理它。
有没有办法真正递归地使用反射类?有什么比我只是不知道的反射类更合适的东西吗?
这是 \Stripe\Plan::all();
的示例 var_dump()
响应:
object(Stripe\Collection)#604 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(4) {
["object"]=>
string(4) "list"
["has_more"]=>
bool(false)
["url"]=>
string(9) "/v1/plans"
["data"]=>
array(2) {
[0]=>
object(Stripe\Plan)#605 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(8) "my_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(9) "My Plan"
["created"]=>
int(1427441577)
["amount"]=>
int(20000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#608 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#612 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#613 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#609 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#610 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
[1]=>
object(Stripe\Plan)#611 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(12) {
["id"]=>
string(9) "some_other_plan"
["interval"]=>
string(5) "month"
["name"]=>
string(14) "Some Other Plan"
["created"]=>
int(1427431129)
["amount"]=>
int(40000)
["currency"]=>
string(3) "usd"
["object"]=>
string(4) "plan"
["livemode"]=>
bool(false)
["interval_count"]=>
int(1)
["trial_period_days"]=>
NULL
["metadata"]=>
object(Stripe\AttachedObject)#614 (5) {
["_opts":protected]=>
object(Stripe\Util\RequestOptions)#603 (2) {
["headers"]=>
array(0) {
}
["apiKey"]=>
string(32) "XXXXXXXXXXXXXXXX"
}
["_values":protected]=>
array(0) {
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#618 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#619 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
["statement_descriptor"]=>
NULL
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#615 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#616 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
}
}
["_unsavedValues":protected]=>
object(Stripe\Util\Set)#606 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_transientValues":protected]=>
object(Stripe\Util\Set)#607 (1) {
["_elts":"Stripe\Util\Set":private]=>
array(0) {
}
}
["_retrieveOptions":protected]=>
array(0) {
}
}
最佳答案
您不必使用反射 API,Stripe\Collection
类实现了 ArrayAccess ,你可以直接遍历它:
$collection = \Stripe\Plan::all();
foreach ($collection as $plan) {
// Do something with the plan
}
Here是 Collection
类扩展的基类。 Stripe 的 PHP 库中的几乎所有类都是如此,包括 Stripe\Plan
。因此,您可以使用与普通数组一起使用的任何类型的递归。
如果您想将 _values
属性作为数组返回,您可以使用 __toArray()
方法:
$array = $collection->__toArray(true);
true
参数是一个递归选项。
关于php - 使用 PHP,如何访问从 Stripe API 返回的 protected _values 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29494535/
这个问题在这里已经有了答案: How to make a property protected AND internal in C#? (8 个答案) 关闭 9 年前。 我需要声明一个既受又 内部保
我有以下代码: class Base{ protected val alpha ="Alpha"; protected def sayHello = "Hello"; } class
我正在尝试运行一个宏来创建一个工作正常的pdf。 Excel 文件获得签名后,Excel 文件将自动受到保护。 问题:我的工作表上有一个按钮,需要按下该按钮才能创建 pdf 文档网。然而,这是无法完成
子类需要能够使用种子随机数生成器。 使用的优点和缺点是什么 public abstract class AbstractClass { protected Random rnd; public
我们有两个类(class)(A 和 B)。 A类只能由继承它的类创建(A类)B 类可以由用户创建。 A 类,版本 1 有一个private 数据成员,并且有方法访问 A 类中的数据。 A 类,版本 2
当父类受到保护时,我对继承类的默认构造函数有疑问,在我看来,子类也会有一个默认构造函数受到保护,但事实并非如此。 除了在子类上强制默认构造函数外,还有其他方法可以强制保护默认构造函数吗? C++11
我有一个抽象类,我想在其 protected 构造函数中初始化一个只读字段。我希望这个只读字段在派生类中可用。 按照我将所有字段设为私有(private)并公开属性的习惯,我实现如下: abstrac
我在运行大数据时遇到错误。错误已由以下示例示例解释 加载数据 mdata <- as.matrix(read.table('https://gubox.box.com/shared/static
我在获取 时遇到问题非法访问错误 对于以下示例: 我在名为 arch 的 gradle 模块中声明了一个基类 abstract class BaseClass { protected abst
我相信通过 实现 JSF 应用程序的安全性+ + & 通过使用 是两种不同的方式!?他们是吗? 我尝试通过上述第一种方法(使用 + + )来实现安全性,但发现使用 protected 和不
有没有办法确定以下的二传手能见度差异: public Prop { get; protected set; } protected Prop { get; set; } 使用反射?还是那些与 C# 反
我读了一本关于 OOP 的书,并且关于“ protected ”访问修饰符的示例对我来说很奇怪。 例子总结 这个例子是为了测试“protected”保留字对变量的影响。 ClassA 有 2 个 pr
内部——在集会上公开,在其他地方私有(private)。 protected - 只有派生类可以访问父类成员。 Protected internal - protected OR internal -
我有一个类代表 Wicket 中带有“返回”、“前进”和“取消”按钮的基本页面。但并非所有页面都有所有按钮,例如。 G。第一页明显没有“返回”。 我的想法是定义一个通用的ActionHandler p
在了解到嵌套类是嵌套类的成员并因此可以完全访问嵌套类的成员这一事实后(至少对于 C++11,请参见 here),我在尝试创建嵌套类模板: #include using namespace std;
我刚刚使用了 Resharper,并一直在尝试将字段转换为属性。我希望这些属性受到保护,但 Resharper 不想给我这个选项。相反,只有一个 protected 内部选项。这让我思考。要么某处有一
这是 question 的扩展一个小时前问过。 当覆盖派生类中的虚方法时,我们不能修改访问修饰符。考虑 System.Web.UI 命名空间中的 Control 类 public class Cont
有人可以解释一下 C# 中 protected 和 protected internal 修饰符之间的区别吗?看起来他们的行为是相同的。 最佳答案 “protected internal”访问修饰符是
我想知道下面两行代码之间是否有区别: protected $var = null; protected $var; 我已经看到两者都被使用了,我想知道这是否只是一个偏好问题,(也就是 $var; 将变
我正在尝试使用mockito为“另存为”操作编写单元测试。该操作的一部分是制作并显示一个文件对话框,用户可以在其中输入要保存的文件。选择文件不是我可以自动化的操作,因此我想模拟 FileDialog
我是一名优秀的程序员,十分优秀!