PHP - Concatenate object class name - php

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

Related

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);

adding to the name of the function some php variable

I have a function called check_nickname()
And I want to write something like this
$ttt='nickname';
check_.$ttt();
How can I do it correct ?
I would advise against that.
this will produce unmaintainable and undebuggable code.
I'd make one function which does all the verifications.
Create a variable that holds the function name, and apply parentheses to it:
$ttt='nickname';
$funcname = 'check_'.$ttt;
$funcname();
You would need to create another variable, such as $a = "check_".$ttt;, then call $a();
$ttt = 'nickname';
$your_function = 'check_' . $ttt;
$your_function();

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

PHP Variable Variables

How does php handle something like this...
$blah = "Testing a variable";
$$blah = "test";
What would my new variable name be?
Everything you need to know about variable variables at http://www.php.net/manual/en/language.variables.variable.php, except for one thing: don't use them.
echo ${'Testing a variable'};
However, you don't want to do this in practice. It makes for unmaintainable, bug-prone code.
The variable $blah must contain a valid variable name.
This will tell you about variables: http://www.php.net/manual/en/language.variables.basics.php
Not really an answer, but...
<?php
function I_love_you()
{
return "haha";
}
$haha = "HoHoHo";
$tom = "I_love_you";
$blah = "tom";
echo ${$$blah()};
?>

PHP object access using string

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"];

Categories