【问题标题】:How do I retrieve an item from a PHP private Object Array?如何从 PHP 私有对象数组中检索项目?
【发布时间】:2012-09-02 18:26:33
【问题描述】:

我到处搜索,IPB 论坛、谷歌,到处都是。

我在 IPB/IP.Nexus 论坛上找不到任何文档,因为他们没有,它是用 Zend 加密的。

我试图做的是从它传递到的函数中的私有对象数组中检索项目“数量”。

这是php文件。

<?php 
class custom_actions_ibeconomy
{
        /**
         * Item Purchased (run before onPurchaseGenerated)
         *
         * @param       array   The member purchasing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @return      void
         */
        public function onPaid( $member, $package, $invoice )
        {

        }

        /**
         * Purchase record generated (run after onPaid)
         *
         * @param       array   The member purchasing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @param       array   Row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onPurchaseGenerated( $member, $package, $invoice, $purchase )
        {
        #What package have we purchased here?
        switch($package['p_id']) {
                        case 3: // 500 HLcoins
                                $value = 500;
                                break;
                        case 4: // 1000 HLcoins
                                $value = 1000;
                                break;

                        default: //Neither? 0 HLcoins.
                                $value = 0;
                                break;;
                }       
        #Find current members HLcoins?           
        $amount = $this->$invoice->quantity * $value;

        #execute points
        ipsRegistry::DB()->update( 'pfields_content', 'eco_points=eco_points+'.$amount, 'member_id=' . $member['member_id'] , true, true );   
        }

        /**
         * Purchase Renewed, but was still active anyway
         *
         * @param       array   The member renewing
         * @param       array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice Invoice Model
         * @param       array   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onRenew( $member, $package, $invoice, $purchase )
        {

        }        

        /**
         * Purchase Renewed after had expired
         *
         * @param       array                   The member renewing
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Invoice Model [May not be set if manually reactivating]
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onReactivate( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase Expired
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Will usually be null. If the purchase is expiring because the invoice which previously renewed it is now being marked unpaid, the invoice object will be passed.
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onExpire( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase Cancelled or Deleted
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       invoice|null    Will usually be null. If the purchase is being cancelled because the invoice which was used to purchase it it is now being marked unpaid, the invoice object will be passed.
         * @param       array                   The row from nexus_purchases [since Nexus 1.5]
         * @return      void
         */
        public function onCancel( $member, $package, $invoice, $purchase )
        {

        }

        /**
         * Purchase is transferred to another member
         *
         * @param       array           Old owner
         * @param       array           Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array           The row from nexus_purchases
         * @param       customer        New owner
         * @return      void
         */
        public function onTransfer( $oldOwner, $package, $purchase, $newOwner )
        {

        }

        /**
         * Purchase is upgraded/downgraded
         *
         * @param       array           The member the purchase belongs to
         * @param       array           Old package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array           The row from nexus_purchases
         * @param       package         New package object
         * @return      void
         */
        public function onchange( $member, $oldPackage, $purchase, $newPackage )
        {

        }

        /**
         * Purchases' parent purchase is changed
         *
         * @param       array                   The member the purchase belongs to
         * @param       array                   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
         * @param       array                   The row from nexus_purchases
         * @param       array|null        The previous parent (may be null if had no parent previously)
         * @param       array|null        The new parent (may be null if was previously associated and now not)
         * @return      void
         */
        public function onAssociate( $member, $package, $purchase, $oldParent, $newParent )
        {

        }

}
?>

但是它什么也没返回。

我已经打印出数组,它被列为私有,但它在函数中,所以我应该能够检索它,我只是不知道如何,我需要你的帮助。

invoice Object
(
    [data:private] => Array
    (
    [i_status] => pend
    [i_title] => 500 HLCoins , 500 HLCoins x8
    [i_member] => 1
    [i_items] => Array
        (
            [0] => Array
            (
    [act] => new
    [app] => nexus
    [type] => product
    [cost] => 0
    [tax] => 0
    [renew_term] => 0
    [renew_units] =>
    [renew_cost] => 0
    [quantity] => 1
    [physical] =>
    [shipping] => Array
        (
            )
        [weight] => 0
        [itemName] => 500 HLCoins
        [itemID] => 3
        [cfields] => Array
(
)
[extra] =>
[opt_id] => 0
[associated] =>
[assocBought] =>
[groupRenewals] => 0
[methods] => Array
(
)
[k] => 0
[_tax] => 0
)
[1] => Array
(
[act] => new
[app] => nexus
[type] => product
[cost] => 0
[tax] => 0
[renew_term] => 0
[renew_units] =>
[renew_cost] => 0
[quantity] => 8
[physical] =>
[shipping] => Array
(
)
[weight] => 0
[itemName] => 500 HLCoins
[itemID] => 3
[cfields] => Array
(
)
[opt_id] => 0
[groupRenewals] => 0
[methods] => Array
(
)
[_tax] => 0
)
)
[i_total] => 0
[i_date] => 1347217384
[i_return_uri] =>
[i_paid] => 0
[i_status_extra] => a:1:{s:4:"type";s:4:"zero";}
[i_discount] => 0
[i_temp] =>
[i_ordersteps] => 0
[i_noreminder] => 1
[i_renewal_ids] => Array
(
)
[i_po] =>
[i_notes] =>
[i_shipaddress] =>
[i_id] => 229
)
[customer:private] => customer Object
(
[data] => Array
(
[member_id] => 1
[name] => Administrator
[member_group_id] => 4
[email] => honlegends@gmail.com
[joined] => 1346519183
[ip_address] => 108.251.14.208
[posts] => 19
[title] => Administrator
[allow_admin_mails] => 0
[time_offset] => -8
[skin] => 5
[warn_level] => 0
[warn_lastwarn] => 0
[language] => 1
[last_post] => 1347048210
[restrict_post] =>
[view_sigs] => 1
[view_img] => 1
[bday_day] => 0
[bday_month] => 0
[bday_year] => 0
[msg_count_new] => 0
[msg_count_total] => 1
[msg_count_reset] => 0
[msg_show_notification] => 1
[misc] =>
[last_visit] => 1347174074
[last_activity] => 1347217377
[dst_in_use] => 1
[coppa_user] => 0
[mod_posts] =>
[auto_track] =>
[temp_ban] => 0
[login_anonymous] => 0&1
[ignored_users] => a:0:{}
[mgroup_others] => 7
[org_perm_id] => ,,,,,8,
[member_login_key] => 341ebfc939f1c5acb5dbe8b9a6819b21
[member_login_key_expire] => 1347820877
[has_blog] =>
[blogs_recache] =>
[has_gallery] => 0
[members_auto_dst] => 1
[members_display_name] => PAPA_bert
[members_seo_name] => papa-bert
[members_created_remote] => 0
[members_cache] => a:7:{s:11:"report_temp";a:0:{}s:19:"report_last_updated";i:1347139096;s:10:"report_num";s:1:"0";s:13:"msgAlertReset";i:1346520781;s:13:"notifications";a:19:{s:13:"report_center";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:11:"new_comment";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:11:"post_quoted";a:1:{s:8:"selected";a:0:{}}s:9:"new_likes";a:1:{s:8:"selected";a:1:{i:0;s:6:"inline";}}s:15:"followed_topics";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:15:"followed_forums";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:22:"followed_topics_digest";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:22:"followed_forums_digest";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:15:"profile_comment";a:1:{s:8:"selected";a:1:{i:0;s:6:"inline";}}s:14:"friend_request";a:1:{s:8:"selected";a:1:{i:0;s:6:"inline";}}s:22:"friend_request_approve";a:1:{s:8:"selected";a:1:{i:0;s:6:"inline";}}s:19:"new_private_message";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:21:"reply_private_message";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:22:"invite_private_message";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:17:"reply_your_status";a:1:{s:8:"selected";a:0:{}}s:16:"reply_any_status";a:1:{s:8:"selected";a:0:{}}s:20:"friend_status_update";a:1:{s:8:"selected";a:0:{}}s:7:"warning";a:1:{s:8:"selected";a:1:{i:0;s:5:"email";}}s:12:"warning_mods";a:1:{s:8:"selected";a:1:{i:0;s:6:"inline";}}}s:23:"show_notification_popup";i:0;s:7:"friends";a:1:{i:11;s:1:"1";}}
[members_disable_pm] => 0
[members_l_display_name] => papa_bert
[members_l_username] => administrator
[failed_logins] =>
[failed_login_count] => 0
[members_profile_views] => 575
[members_pass_hash] => cf407e695cc8d14e0a7079c8e0e1f4b4
[members_pass_salt] => ]s:%5
[member_banned] => 0
[member_uploader] => flash
[members_bitoptions] => 0
[fb_uid] => 0
[fb_emailhash] =>
[fb_lastsync] => 0
[members_day_posts] => 0,0
[live_id] =>
[twitter_id] =>
[twitter_token] =>
[twitter_secret] =>
[notification_cnt] => 0
[tc_lastsync] => 0
[fb_session] =>
[fb_token] =>
[ips_mobile_token] =>
[unacknowledged_warnings] =>
[feedb_percent] => -1
[feedb_pos] => 0
[feedb_neg] => 0
[feedb_neu] => 0
[sospromote_vip] => 0
[sospromote_vip_eterno] => 0
[sospromote_vip_g_origem] => 0
                    [sospromote_vip_dias] => 0
                    [sospromote_vip_secondary] => 0
                    [sospromote_vip_removesecondary] => 0
                    [sub_end] => 0
                    [subs_pkg_chosen] => 0
                    [cm_credits] => 0
                    [cm_reg] => 0
                    [referred_by] => 0
                    [cm_no_sev] => 0
                    [cim_profile_id] => 
                    [cim_payment_id] => 0
                    [cim_method] => 0
                    [cm_return_group] => 0
                    [my_member_id] => 1
                    [pp_member_id] => 1
                    [pp_last_visitors] => a:2:{i:1346978590;i:7;i:1346536186;i:2;}
                    [pp_rating_hits] => 0
                    [pp_rating_value] => 0
                    [pp_rating_real] => 0
                    [pp_main_photo] => http://www.honlegends.com/community/uploads/profile/photo-1.jpg?_r=1347002709
                    [pp_main_width] => 170
                    [pp_main_height] => 125
                    [pp_thumb_photo] => http://www.honlegends.com/community/uploads/profile/photo-thumb-1.jpg?_r=1347002709
                    [pp_thumb_width] => 100
                    [pp_thumb_height] => 100
                    [pp_setting_moderate_comments] => 0
                    [pp_setting_moderate_friends] => 0
                    [pp_setting_count_friends] => 1
                    [pp_setting_count_comments] => 1
                    [pp_setting_count_visitors] => 1
                    [pp_about_me] => 
                    [pp_reputation_points] => 1
                    [pp_gravatar] => 
                    [pp_photo_type] => custom
                    [signature] => [center][img]http://tomeisopa.files.wordpress.com/2009/11/snooze-snorlax.png[/img][/center]
                    [avatar_location] => 
                    [avatar_size] => 0
                    [avatar_type] => 
                    [pconversation_filters] => 
                    [fb_photo] => 
                    [fb_photo_thumb] => 
                    [fb_bwoptions] => 0
                    [tc_last_sid_import] => 0
                    [tc_photo] => 
                    [tc_bwoptions] => 0
                    [pp_customization] => 
                    [pp_profile_update] => 1347002709
                    [cache_content] => <p class='bbc_center'><span rel='lightbox'><img src='http://tomeisopa.files.wordpress.com/2009/11/snooze-snorlax.png' alt='Posted Image' class='bbc_img' /></span></p>
                    [full] => 1
                    [_canBeIgnored] => 
                    [bw_is_spammer] => 0
                    [bw_from_sfs] => 0
                    [bw_vnc_type] => 0
                    [bw_forum_result_type] => 0
                    [bw_no_status_update] => 0
                    [bw_status_email_mine] => 0
                    [bw_status_email_all] => 0
                    [bw_disable_customization] => 0
                    [bw_local_password_set] => 0
                    [bw_disable_tagging] => 0
                    [bw_disable_prefixes] => 0
                    [bw_using_skin_gen] => 0
                    [bw_disable_gravatar] => 0
                    [_group_formatted] => <span style="color: #46c843; font-weight: bold; text-shadow: 0 0 .7em #59ff55;">Administrators</span>
                    [member_rank_img] => http://www.honlegends.com/community/public/style_extra/team_icons/admin.png
                    [member_rank_img_i] => img
                    [show_warn] => 1
                    [custom_fields] => 
                    [_has_photo] => 1
                    [pp_small_photo] => http://www.honlegends.com/community/uploads/profile/photo-thumb-1.jpg?_r=1347002709
                    [pp_small_width] => 50
                    [pp_small_height] => 50
                    [pp_mini_photo] => http://www.honlegends.com/community/uploads/profile/photo-thumb-1.jpg?_r=1347002709
                    [pp_mini_width] => 25
                    [pp_mini_height] => 25
                    [_online] => 1
                    [_last_active] => Today, 12:02 PM
                    [_pp_rating_real] => 0
                    [members_display_name_formatted] => <span style="color: #46c843; font-weight: bold; text-shadow: 0 0 .7em #59ff55;">PAPA_bert</span>
                    [members_display_name_short] => PAPA_bert
                    [author_reputation] => Array
                        (
                            [text] => Neutral
                            [image] => 
                        )

                    [member_title] => Administrator
                    [cm_first_name] => Albert
                    [cm_last_name] => Yang
                    [cm_address_1] => 2919 Warwick Ave
                    [cm_address_2] => 
                    [cm_city] => Los Angeles
                    [cm_state] => CA
                    [cm_zip] => 90032
                    [cm_country] => US
                    [cm_phone] => 3233160892
                    [_name] => Albert Yang
                )

            [cardhandler:private] => 
        )

    [takeAction] => 1
)

【问题讨论】:

  • 您可以使用 Reflexion API,但我不鼓励您尝试这样做。

标签: php arrays object private


【解决方案1】:

理论上:

ob_start();
var_dump($object);
$objectStr = ob_get_clean();

然后使用字符串函数获取值 - 类似于:

$qPos = strpos($objectStr,"quantity",0);
$value = substring($objectStr$qPos+SOMEVALUE,$qPOS+SOMEVALUE+LENGTH);

这不是一个完美的答案,您必须对其进行调整。

然而,正如@abhi-beckert 所说,这样做很糟糕,而且性能可能很糟糕。检查文档,看看是否有不同的方法。

您不能扩展类来做到这一点 - 私有不会被继承。

【讨论】:

    【解决方案2】:

    无法访问私有属性。只有像var_dump() 这样的调试工具才能看到它们。

    您应该找到 invoice 类的定义并添加一个新函数来编辑该属性(假设它还没有)或将其更改为 public。

    【讨论】:

      【解决方案3】:

      您可以扩展该类,并创建一个访问器方法(或“getter”)来检索值:

      <?php
      class YourClassName extends custom_actions_ibeconomy
      {
          public function getQuantity()
          {
              return $this->quantity;
          }
      }
      

      【讨论】:

      • 谢谢各位,我会调查的。
      • 我的错。正如@Philip Whitehouse 所说,私有属性不会在扩展类中继承,只有公共和受保护的属性是。哦!
      猜你喜欢
      • 2012-09-02
      • 2013-06-22
      • 1970-01-01
      • 2016-02-24
      • 2011-07-07
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      相关资源
      最近更新 更多