How to Access a PHP Object Attribute Having a Dollar Sign

How do I access a PHP object attribute having a dollar sign?

  1. With variable variables:

    $myVar = 'variable$WithDollar';
    echo $object->$myVar;
  2. With curly brackets:

    echo $object->{'variable$WithDollar'};

How do I access a PHP object attribute having a @ sign?

Its is attributes see SimpleXMLElement::attributes for more information

Example

foreach($xml->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}

PHP property access and dollar sign

A class constant is accessed with the :: scope operator, and no dollar sign, so the $ is needed there to differentiate between static class properties and class constants.

class myClass {
public static $staticProp = "static property";

const CLASS_CONSTANT = 'a constant';
}

echo myClass::CLASS_CONSTANT;
echo myClass::$staticProp;

So to access a variable, the $ is necessary. But the $ cannot be placed at the beginning of the class name like $myClass::staticProp because then the class name could not be identified by the parser, since it is also possible to use a variable as the class name. It must therefore be attached to the property.

$myClass = "SomeClassName";
// This attempts to access a constant called staticProp
// in a class called "SomeClassName"
echo $myClass::staticProp;

// Really, we need
echo myClass::$staticProp;

Access object property with disallowed character in property name

This will work:

echo $object->feed->{'xmlns$media'};

Alternatively, you can tell json_decode to return an array:

$array = json_decode($json, true);
echo $array['feed']['xmlns$media'];

Why doesn't PHP need $ when accessing object properties?

We don't use $ because, there is no risk to have a constant with the same name because class constants are called statically (Class::const) and that's why, when we call a static property we use $ (Class::$property)

PHP Access Object Property with @ at the rate symbol

Wrap it into {}:

$ABC->{"@total"}; // here it needs to insert quotes as well

Also note that you have to prefix your class object with the dollar sign, this is the link to sandbox example.

Accessing object variable via double dollar sign

$this->data->$this->iterator is ambiguous. Do you mean the property of $this->data that has the name of $this->iterator or the iterator property of $this->data->$this?

You need to make it unambiguous:

$this->data->{$this->iterator}

The same goes for $$this->iterator.

Access a PHP-object with dollar-sign as node name - Parse error: syntax error, (T_CONSTANT_ENCAPSED_STRING)

First, there is no yt$googlePlusUser, it's yt$googlePlusUserId.

Then you've missed one level. Instead of

$artist_object->{'yt$googlePlusUserId'}

use

$artist_object->entry->{'yt$googlePlusUserId'}

And finally, as a bonus, to get the ID:

$artist_object->entry->{'yt$googlePlusUserId'}->{'$t'}

Alternatively as it's written in the answer to the question you referred to you could convert the JSON object to an array using $artist = json_decode($artist_json, true) and access the property as $artist['entry']['yt$googlePlusUserId']['$t].

Update:
Regarding PHP version, etc. you seem to have 5.4.19 installed, I've tested the above on 5.4.0 and it works.

Accessing PHP object properties when property name has non allowed chars

$propertyName = 'property-name';

$phpObj->{$propertyName} = ...
$phpObj->{"property-name"} = ...

only this may be problem:

$phpObj->First-Prop and $phpObj->first-prop 

variable/function/method/property definitions in php are not case-sensitive...



Related Topics



Leave a reply



Submit