Viewed   82 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?

 Answers

2

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.

Sunday, October 16, 2022
2

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.

Saturday, September 3, 2022
5

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;
$getopt->addOptions(array(
    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),
));

$getopt->parse();
Wednesday, August 17, 2022
 
5

If you want to avoid the warning you'll need to pre-create each level:

$data = new stdClass();
$data->result = new stdClass();
$data->result->complex = new stdClass();
$data->result->complex->first_key = $old_data->result->complex;
Tuesday, August 30, 2022
 
naim
 
4

These 3 simple steps did the work:

//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );

Note: Answer by @Kamran helped me come to this simple solution

Friday, August 26, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :