How to get protected property of object in PHP

How to get protected property of object in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Accessing Protected Properties in PHP Without Reflection

When working with legacy frameworks or third-party libraries, you often encounter objects that encapsulate their internal state behind protected properties. The example provided shows a Fields_Form_Element_Location instance where the _value property holds the string "Current City" (or an integer like 93399 depending on context), but direct access via $obj->_value or public getter methods fails. This is by design; the class intentionally hides its state to prevent external modification through the object graph.

The standard modern approach to bypass this barrier is using the ReflectionClass. You instantiate a reflection object for the target class, retrieve the property via name, and then use get() or setValue(). While effective in environments running PHP 5.3 or later, this solution hits an immediate wall on servers running older versions like PHP 5.2.17, as mentioned in the source scenario. The core issue is that ReflectionProperty did not support accessing protected properties of objects from outside the class hierarchy in PHP 5.2 without additional workarounds or specific extensions.

If you are strictly bound to a server environment running PHP 5.2.17, you cannot rely on standard reflection APIs to access these properties directly. However, there is a viable alternative: modifying the object's visibility. In PHP, protected and private modifiers can be temporarily changed using ReflectionProperty even in older versions, but only if you have write access to the property descriptor. The trick involves creating a new ReflectionClass, getting the ReflectionProperty, setting its accessibility to public, and then accessing it.

Here is how you can implement this workaround without upgrading your PHP version immediately:

<?php
// Assuming $obj is your Fields_Form_Element_Location instance

$reflection = new ReflectionClass($obj);
$property = $reflection->getProperty('_value');

// Force the property to be public
$property->setAccessible(true);

// Now you can read and write it safely
$currentValue = $property->getValue($obj);
echo $currentValue; // Outputs: 93399 (or whatever value is set)

// To set a new value
$newValue = 'New Location';
$property->setValue($obj, $newValue);
?>

The critical line here is $property->setAccessible(true). This method tells the reflection engine to ignore the visibility modifiers (public, protected, or private) for the duration of that specific access operation. While this violates encapsulation principles, it is a necessary evil when dealing with legacy systems where you cannot modify the source code of the library providing the object.

It is important to note that setAccessible was introduced in PHP 5.2.0, which means your target server version (5.2.17) actually supports this feature natively. The confusion often arises because developers assume older versions lack reflection support entirely or that ReflectionProperty behaves differently regarding visibility. In reality, as long as you are on 5.2.17 or higher, this pattern works reliably.

When applying this technique to a live server, consider the security implications. Accessing protected properties allows you to inspect internal state that the library author intended to keep hidden. If your application relies on these objects for validation logic (like the _allowEmpty flag shown in your dump), blindly reading their internal values might expose vulnerabilities if those values are later serialized or logged without sanitization.

For modern PHP projects, this workaround is rarely necessary. Frameworks like Laravel provide robust accessor and mutator patterns that handle protected properties cleanly. If you are maintaining a legacy codebase where you cannot upgrade PHP 5.2 to a supported version, this reflection-based approach remains the most pragmatic solution. However, if you have control over the environment, upgrading to PHP 7.4 or later is strongly recommended to avoid relying on such fragile workarounds and to benefit from strict typing and better security defaults.

If you are building new features on top of existing legacy components, consider creating a dedicated service layer that wraps these objects. This layer can expose a clean public API (e.g., getLocationValue()) that internally uses reflection to extract the data. This isolates the "hacky" logic from your main business logic and makes the codebase easier to audit and maintain in the long run.

Our network