0

Fatal error: Cannot access protected property Some_Plugin::$_some_property

What is the best/least-destructive way of accessing a protected property attached to a post.

The property is being affixed to the post by a plugin but I need to access it's stored information to display.

The options I can think of are:

  1. Altering the plug-in code. I'm not so familiar with Object Oriented PHP so I hesitate to do this to avoid damaging the functionality.
  2. Somehow invoke the plug-in so I'm in the correct context to retrieve the information (possibly with function in functions.php)
  3. Write a function which hooks to the publishing of this post type and duplicates the information to a new, non-protected property of the post.

Does anyone have any suggestions or code samples illustrating the best way to go about this. If you need more information just comment and I'll update the question.

Thanks!

3
  • Have you contacted support for that plugin?
    – Tom J Nowell
    Mar 26, 2015 at 17:32
  • @TomJNowell, no, I haven't. It's a tiered plug-in with free and payed versions. Fairly certain support is only available for paying users.
    – rob-gordon
    Mar 26, 2015 at 17:38
  • It's just that depending on how that protected member variable got there could change the answer to the question. There is a generic way of doing it but if you're having trouble understanding OOP then the generalised reflection based answers are simply beyond your skill level to understand. Knowing more about the code would provide alternatives, but since 3rd party plugins are offtopic and you asked a general answer I can only answer in the generic case
    – Tom J Nowell
    Mar 26, 2015 at 17:43

2 Answers 2

1

The foolproof method here to grab a dynamic member variable is to use reflection!

Lets say we have this class:

class MyClass {
     private $myProperty = true;
}

We can use reflection to acquire the class, and the property:

$class = new ReflectionClass("MyClass");
$property = $class->getProperty("myProperty");

We can then set that property to accessible:

$property->setAccessible(true);

Now we can access the private member variable using the new $property object:

$obj = new MyClass();
echo $property->getValue($obj); // Works

Note, that the member variable is still private if we access it directly:

echo $obj->myProperty; // Error

However your code implies a static member variable, e.g.:

class Some_Plugin
    private static $_some_property;
}

Which this may not work for

0

Thank you @TomJNowell, I didn't end up using a reflection class but your answer had enough information regarding classes to get me on the right track. My steps were this.

I searched the plugin directory's php files for lines containing the property I wanted to use in my template.

grep --include=\*.php -rnw '/path/to/plugin' -e "_some_property"

I searched through the lines and eventually found the property being called kind of like this:

$property = Plugin_Registry::instance()->load_model('Someproperty' )->get_property( $post_id );

In my template file I then tested to see if I had access to the Plugin_Registry class.

class_exists(Plugin_Registry) // = 1

Then I was able to just copy that line directly into my template and have all the properties I was looking for.

1
  • Actually I spoke too soon... this was the first part of the answer. But even once I got access to the Class containing the property it required using ReflectionClass exactly as described in the answer above.
    – rob-gordon
    Mar 26, 2015 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.