Wednesday, June 05, 2013

Accessing simple private fields in PHP

A way of accessing private fields in PHP is by changing the accessibility of the fields themselves with
However this approach requires PHP 5.3.

For PHP < 5.3 there's another subtle way of accessing private properties:

function getPrivateProperty($fixture, $propname) {
    try {
        $arr = (array)$fixture;
    } catch (Exception $e) {
    $class = get_class($fixture);
    $privname = "\0$class\0$propname";
    return $arr[$privname];

Usage is pretty straightforward, pass in the object and the property name as string. The property must be private and must be convertible to array.


DedicatedHosting4u said...

This is really a big and great source of information. We can all contribute and benefit from reading as well as gaining knowledge from this content just amazing
experience Thanks for sharing such a nice information.

MindtechAffiliates said...

Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information.

Cpa offers

pandith13 said...

This is a good article. Thanks for sharing

Vasudeva said...

Great post, thanks
SRIKRISHANA ASTROLOGY.Vashikaran Astrologer in Raichur

abhiramindia said...

Very good article,Thank you

ABHIRAM ASTROLOGY CENTER.Best indian Astrologer In lake-louise

DurgaAnugarha said...

Nice post.Keep sharing. Thanks for sharing.

DURGAANUGARHA ASTROLOGY.vashikaran Astrologer In davangere

abhiramindia said...

it was great information and very useful

ABHIRAM ASTROLOGY CENTER.Best Astrologer In grande-prairie