PHP object access using string - php

I have an object in PHP with some very odd property names. I just need to know how to access a property when it's name is "//www.w3.org/1999/02/22-rdf-syntax-ns#type".
I found something that suggested
$object->{'//www.w3.org/1999/02/22-rdf-syntax-ns#type'};
but that doesn't seem to work.
Thanks in advance
Rob

Your example works for me (PHP 5.2.9 and 4.4.4):
class A
{
}
$a = new A();
$p = '//www.w3.org/1999/02/22-rdf-syntax-ns#type';
$a->$p = 'wtf';
echo $a->{'//www.w3.org/1999/02/22-rdf-syntax-ns#type'};
echo $a->$p;

Have you tried :
get_object_vars($object)["//www.w3.org/1999/02/22-rdf-syntax-ns#type"];

Related

PHP - Concatenate object class name

Is is possible to concatenate an object's name?
The below doesn't seem to work..
Trying to call $node->field_presenter_en;
$lang = 'en';
$node->field_presenter_.$lang;
${$node->field_presenter_.$lang};
Thanks!
Try:
$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
This is called variable variables. More information here:
http://php.net/manual/en/language.variables.variable.php
Edit:
The user nickb has suggested a much more elegant solution below, and I will incorporate into this answer for easier reading (nickb: please let me know if you want me to remove this):
$node->{'field_presenter_'.$lang}
$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
<?php
class A {
public $prop = 'hello';
}
$a = new A();
echo $a->{'pro' . 'p'}; // hello

Determine class of an object

It know it can be done with get_class($variable).
The problem is that my $object is actually a string containing the variable name.
so:
$object = new MyClass();
$var = '$object';
$class = get_class($var); // obviously fails
I can't use get_class($object), because I don't have direct access to that variable (I'm producing the $var string from parsing a PHP expression using token_get_all())
I tried using eval(sprintf('return get_class(%s);', $var)), but it doesn't work because the variable appear undefined from eval's scope :(
Is there a way to do this?
I need to know the class in order to pass it to ReflectionMethod, so I can get information about a method (the next element in the PHP expression).
NVM: I'm pretty sure it is not possible. Sorry for asking:)
you can do
$var = new $object();
Try using variable variables: http://php.net/manual/en/language.variables.variable.php
Something like:
$var = 'object';
$class = get_class( $$var );
you can do the following
$ref = ltrim($var, '$');
get_class($ref);

Call function from an object?

<?php
$ar = (object) array('a'=>function(){
echo 'TEST';
});
$ar->a();
?>
I get this error Call to undefined method
Update:
If you are using PHP 5.3 or greater, take a look at other answers please :)
I don't think that's correct syntax, it would give you:
Parse error: syntax error, unexpected T_FUNCTION in....
You need to create a class, add method to it, use new keyword to instantiate it and then you will be able to do:
$ar->a();
class myclass
{
public function a()
{
echo 'TEST';
}
}
$ar = new myclass;
$ar->a(); // TEST
See Classes and Objects for more information.
Anonymous or not, you have a callback function, thus you need to handle it as such. E.g.:
<?php
$ar = (object) array(
'a' => function(){
echo 'TEST';
}
);
call_user_func($ar->a);
?>
For some reason it doesn't seem possibly to run the closure the way you do.
If you modify your code and set another variable to the function, it can be called:
$ar = (object) array('a'=>function(){
echo 'TEST';
});
$a = $ar->a;
$a();
This is no solution. But from what I can see, this seems like a bug or limitation in PHP 5.3.
I am using 5.3.5 when trying this.
There is no function a() but the property a, so you should call it by $ar->a.
Anyway I don't think it's going to work the way you expect it to.
EDIT:
As suggested by Álvaro G. Vicario you should use call_user_func, not echo to call the function and it will work correctly.
Or, just for the fun of it, you can do something like this -
<?php
$ar = new stdClass;
$ar->a = function($to_echo){ echo $to_echo; };
$temp = $ar->a;
//[Edit] - $ar->a("hello"); // doesn't work! php tries to match an instance method called "func" that is not defined in the original class' signature
$temp("Hey there");
call_user_func($ar->a("You still there?"));
?>

php newbie question: classes

i'm new to php and am wondering - is it possible to initialize a class with parameters?
like $obj = new myClass('myID');
i've tried but it gave me an error.
maybe someone can point me to some good tutorials.
thanks
Yes it is, you need to pass the variables through the constructor:
class SomeClass
{
function __construct($some_var)
{
}
}
Please note that in older versions of php the constructor needs to have the name of the class, it´s __construct() since php 5.
Yes, you need to use a class constructor.
Example:
<?php
class ClassWithArgs
{
function __construct($argument1, $argument2)
{
echo "arg 1 = $argument1\n";
echo "arg 2 = $argument2\n";
}
}
$object = new ClassWithArgs('one', 'two');
?>
Example Output
arg 1 = one
arg 2 = two
I suggest you also take a look at the PHP5 OOP documentation, it includes simple examples to get you going: http://php.net/manual/en/language.oop5.php

How can I access an object property named as a variable in php?

A Google APIs encoded in JSON returned an object such as this
[updated] => stdClass Object
(
[$t] => 2010-08-18T19:17:42.026Z
)
Anyone knows how can I access the $t value?
$object->$t obviously returns
Notice: Undefined variable: t in /usr/local/...
Fatal error: Cannot access empty property in /....
Since the name of your property is the string '$t', you can access it like this:
echo $object->{'$t'};
Alternatively, you can put the name of the property in a variable and use it like this:
$property_name = '$t';
echo $object->$property_name;
You can see both of these in action on repl.it: https://repl.it/#jrunning/SpiritedTroubledWorkspace
Correct answer (also for PHP7) is:
$obj->{$field}
Have you tried:
$t = '$t'; // Single quotes are important.
$object->$t;
I'm using php7 and the following works fine for me:
class User {
public $name = 'john';
}
$u = new User();
$attr = 'name';
print $u->$attr;
this works on php 5 and 7
$props=get_object_vars($object);
echo $props[$t];

Categories