PHP - 如何从对象中检索值

时间:2013-02-07 07:33:02

标签: php arrays object

我一直在拉这个头发,包括在这里回顾问题(比如这个问题:Read var_dump data with PHP),但我只是没有得到它。

我正在尝试检索以下值:

COM_USERS_REGISTRATION_SAVE_FAILED

并将其存储为变量。这是我试图从(它是文本的最后一行)检索它的绝对混乱(我不明白):

object(UsersModelRegistration)#98 (9)
    { ["data:protected"]=> object(stdClass)#106 (1) { 
        ["groups"]=> array(1) {
            [0]=> string(1) "2" } }         
    ["_forms:protected"]=> array(0) { }
    ["__state_set:protected"]=> NULL
    ["_db:protected"]=> object(JDatabaseMySQL)#15 (19) {
        ["name"]=> string(5) "mysql"
        ["nameQuote:protected"]=> string(1) "`"
        ["nullDate:protected"]=> string(19) "0000-00-00 00:00:00"
        ["dbMinimum:protected"]=> string(5) "5.0.4"
        ["_database:private"]=> string(15) "table_joomla"
        ["connection:protected"]=> resource(29) of type (mysql link)
        ["count:protected"]=> int(0)
        ["cursor:protected"]=> resource(83) of type (Unknown)
        ["debug:protected"]=> bool(false)
        ["limit:protected"]=> int(0)
        ["log:protected"]=> array(0) { }
        ["offset:protected"]=> int(0)
        ["sql:protected"]=> object(JDatabaseQueryMySQL)#126 (18) {
            ["db:protected"]=> *RECURSION*
            ["type:protected"]=> string(6) "select"
            ["element:protected"]=> NULL
            ["select:protected"]=> object(JDatabaseQueryElement)#127 (3) {
                ["name:protected"]=> string(6) "SELECT"
                ["elements:protected"]=> array(1) {[0]=> string(4) "`id`" }
                ["glue:protected"]=> string(1) "," }
            ["delete:protected"]=> NULL
            ["update:protected"]=> NULL
            ["insert:protected"]=> NULL
            ["from:protected"]=> object(JDatabaseQueryElement)#128 (3) {
                ["name:protected"]=> string(4) "FROM"
                ["elements:protected"]=> array(1) {
                    [0]=> string(10) "`#__users`" }
            ["glue:protected"]=> string(1) "," }
            ["join:protected"]=> NULL
            ["set:protected"]=> NULL
            ["where:protected"]=> object(JDatabaseQueryElement)#129 (3) {
                ["name:protected"]=> string(5) "WHERE"
                ["elements:protected"]=> array(2) {
                    [0]=> string(31) "`username` = 'test123@mail.com'"
                    [1]=> string(9) "`id` != 0" }
            ["glue:protected"]=> string(5) " AND " }
            ["group:protected"]=> NULL ["having:protected"]=> NULL
            ["columns:protected"]=> NULL ["values:protected"]=> NULL
            ["order:protected"]=> NULL ["union:protected"]=> NULL
        ["autoIncrementField:protected"]=> NULL }
        ["tablePrefix:protected"]=> string(6) "s7kou_"
        ["utf:protected"]=> bool(true)
        ["errorNum:protected"]=> int(0)
        ["errorMsg:protected"]=> string(0) ""
        ["hasQuoted:protected"]=> bool(false)
    ["quoted:protected"]=> array(0) { } }
    ["name:protected"]=> string(12) "registration"
    ["option:protected"]=> string(9) "com_users"
    ["state:protected"]=> object(JObject)#99 (1) {
        ["_errors:protected"]=> array(0) { } }
    ["event_clean_cache:protected"]=> string(19) "onContentCleanCache"
    ["_errors:protected"]=> array(1) {
        [0]=> string(34) "COM_USERS_REGISTRATION_SAVE_FAILED" } }

2 个答案:

答案 0 :(得分:0)

对象的所有属性都受到保护,要访问它们,您应该对UsersModelRegistration类实现“getters”/或者扩展UsersModelRegistration并实现getter

/**
* @returns array
*/
public function getErrors(){

return $this->_errors;
}

答案 1 :(得分:0)

看看类源,很可能有一个返回此值的方法。 解决方案来访问受保护的属性(Reflections或将对象转换为数组)我不会推荐这些,因为它们完全打破了OOP原则封装。该属性为protected,原因是您应该通过UserModelRegistration的公共接口访问它。