Asked  7 Months ago    Answers:  5   Viewed   40 times

I have a object having some protected property that I want to get and set. The object looks like

Fields_Form_Element_Location Object
[helper] => formText
[_allowEmpty:protected] => 1
[_autoInsertNotEmptyValidator:protected] => 1
[_belongsTo:protected] => 

[_description:protected] => 
[_disableLoadDefaultDecorators:protected] => 
[_errorMessages:protected] => Array

[_errors:protected] => Array
[_isErrorForced:protected] => 
[_label:protected] => Current City

[_value:protected] => 93399
[class] => field_container field_19 option_1 parent_1

I want to get value property of the object. When I try $obj->_value or $obj->value it generates error. I searched and found the solution to use PHP Reflection Class. It worked on my local but on server PHP version is 5.2.17 So I cannot use this function there. So any solution how to get such property?



That's what "protected" is meant for, as the Visibility chapter explains:

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

If you need to access the property from outside, pick one:

  • Don't declare it as protected, make it public instead
  • Write a couple of functions to get and set the value (getters and setters)

If you don't want to modify the original class (because it's a third-party library you don't want to mess) create a custom class that extends the original one:

class MyFields_Form_Element_Location extends Fields_Form_Element_Location{

... and add your getter/setter there.

Wednesday, March 31, 2021
answered 7 Months ago

In reality it is often helpful to know the actual called (sub)class when executing a superclass method, and I disagree that there's anything wrong with wanting to solve this problem.

Example, my objects need to know the class name, but what they do with that information is always the same and could be extracted into a superclass method IF I was able to get the called class name. Even the PHP team thought this was useful enough to include in php 5.3.

The correct and un-preachy answer, as far as I can tell, is that prior to 5.3, you have to either do something heinous (e.g. backtrace,) or just include duplicate code in each of the subclasses.

Wednesday, March 31, 2021
answered 7 Months ago

Use $this and an array as the callback:

array_map( array( $this, 'mapKeyValue'), $arr, $arr2);

And, just to be sure, this is tested with PHP 5.2.17 and is confirmed working.

Wednesday, March 31, 2021
answered 7 Months ago

I just found a nearly aproximation to python argparse library. The php library Getopt.PHP partialy emulates a parser with several features and same behaviour in argparse.

For example add_options() ~= addOptions() method with positional or required arguments, automatic usage generation, retrieval values, error handling, etc.

At this moment, it still needs some work for more advanced features like conflict handler, choices or metavar, parse known values only, parameter exclusion, nargs, etc. But I can easily extended or use it as good implementation base start.

The Python example code in my question could be nearly translated (with missing features) to:

$getopt = new Getopt;
    array('o', 'output', Getopt::OPTIONAL_ARGUMENT, 'Set the output directory'),
    array('q', 'quiet', Getopt::OPTIONAL_ARGUMENT, 'Don't print status messages to stdout'),
    array('v', 'version', Getopt::OPTIONAL_ARGUMENT, 'Shows the program version'),
    array('FILES', NULL, Getopt::REQUIRED_ARGUMENT),

Wednesday, March 31, 2021
answered 7 Months ago

References to the same objects already get copied when you copy the array. But it sounds like you want to shallow-copy deep-copy the objects being referenced in the first array when you create the second array, so you get two arrays of distinct but similar objects.

The most intuitive way I can come up with right now is a loop; there may be simpler or more elegant solutions out there:

$new = array();

foreach ($old as $k => $v) {
    $new[$k] = clone $v;
Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :