我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

php - 访问类对象属性,如关联数组

so i'm having a php class like the following:

class myClass
{
    function __construct()
    {
        $this->chart_data = array(1,2,3,4,5);
        $this->captions   = array("a", "b", "c", "d", "e");
    }
}

is there a way to access the properties the same way like an associative array, like:

$obj = new myClass();
echo $obj['chart_data'];
echo $obj['captions'];

thanks!

From this answer you can try like this

$obj = (array) new myClass();
print_r($obj);

you can try

echo $obj->{'chart_data'};
echo $obj->{'captions'};

近期文章

  • Ajax / jQuery / PHP使用请求设置Cookie
  • 如何设置多个apache虚拟主机?
  • Php PDO与数据库的连接
  • 在节点中打印帐户链接字段
  • 无论用户是否登录,$ logged都返回1
  • PHP shell_exec似乎失败了
  • 使用php的tcpdf额外空白页面
  • 使用PHP和oracle数据库执行在另一个表中存储为列的查询
  • 将div连接成报废内容
  • 使用Android API为Android App创建Web前端。
  • 如何在收音机复选框中制作错误表单并在codeigniter中下拉
  • PHP中$ _FILES的问题
  • 在PHP中从远程文件获取XML文档模式
  • Sugarcrm 6.1.2 Pro External Accounts选项卡未出现在编辑用户视图中
  • 将输入值传递给另一个URL上的另一个表单并运行submit wordpress
  • 道路坐标缓存(打破谷歌的T&C?)
  • WooCommerce令人讨厌的描述
  • 在表格的下拉列表中分配变量值
  • 无法在laravel4中运行mysql存储过程
  • 如何为我的应用程序创建索引数据库

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华