Asked  1 Year ago    Answers:  5   Viewed   15 times

Regarding PHP, is there a way to change the order of object properties?

class o {public $a = 1, $b = 2;}
$o = new o;
foreach (get_object_vars($o) as $k => $v) {
  print $k . '->' . $v . PHP_EOL;



Existing public variables can be unset(), and added e.g. with $o->c = 3;. But array functions do not work with objects, and I do not want to convert the object to some stdClass.

The only practical workaround I can think of is to decorate an array object and overload the magic __get() and __set() methods, but that is just a workaround, not a solution.



You can implement your own way to iterate over object just by implementing Iterator interface. By implementing methods next and current you define how to get current element and how to get the next one (but you will have to implement all methods).

For iteration use

foreach ($o as $k => $v) {
  print $k . '->' . $v . PHP_EOL;

Care to see some example? Did you understand it from link above?

On the other hand if you want to use your object as array, check ArrayObject interface or for simplier use ArrayAccess interface

Thursday, April 1, 2021
  1. $property1 // specific variable
  2. $this->property1 // specific attribute

The general use on classes is without "$" otherwise you are calling a variable called $property1 that could take any value.


class X {
  public $property1 = 'Value 1';
  public $property2 = 'Value 2';
$property1 = 'property2';  //Name of attribute 2
$x_object = new X();
echo $x_object->property1; //Return 'Value 1'
echo $x_object->$property1; //Return 'Value 2'
Thursday, April 1, 2021

This is more along the lines of they way that you didn't want to do it....

$extended = (object) array_merge((array)$obj, (array)$obj2);

However I think that would be a little better than having to iterate over the properties.

Thursday, April 1, 2021

console.log does indeed sort the properties, in some cases you can use JSON.stringify which preserves the order, e.g.

console.log(JSON.stringify(obj, null /*replacer function */, 4 /* space */))

NB: contrary to the popular belief, js objects maintain the enumeration order, as per the OwnPropertyKeys specification (integers first, then other properties in insertion order)

Friday, July 30, 2021

If your figure was generated in R2014b or newer you can utilize the undocumented 'PlotChildren' property to manipulate the order of the legend entries without requiring a new legend call.

For example:

x = 1:10;
y1 = x;
y2 = 2*x;
y3 = 3*x;
y4 = x.^2;

plot(x, y1, x, y2, x, y3, x, y4);
lh = legend('y = x', 'y = 2*x', 'y = 3*x', 'y = x.^2');


Which you can then manipulate:

neworder = [3, 1, 4, 2];
lh.PlotChildren = lh.PlotChildren(neworder);


If you don't have the handle to the legend object, it is a child of the figure object containing the axes object your data is plotted on. You can find the handle to your legend object using one of the following findobj approaches:

% Handle to figure object known
lg = findobj(figureobj, 'Type', 'legend');

% Handle to figure object unknown
lh = findobj(gcf, 'Type', 'legend');

Note that gcf generally returns the handle to the last figure that the user clicked on, but this is not necessarily always the case.

Self promotion edit: This method is included in a set of legend manipulation tools maintained on GitHub by the StackOverflow MATLAB community.

Monday, November 29, 2021
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 :