I want to know how can I concatenate [und][0][value].
I don't want to write every time [und][0][value]. So I have do like this:
<?php
$und_value = $load->field_testimonial_location['und'][0]['value'];
$query = db_select('node','n');
$query->fields('n',array('nid'));
$query->condition('n.type','testimonial','=');
$result = $testimonial_query->execute();
while($fetch = $result->fetchObject()){
$load = node_load($fetch->nid);
// $location = $load->field_testimonial_location['und'][0]['value'];
$location = $load->field_testimonial_location.$und_value;
echo $location;
}
But its not working. It outputs Array Array So have any idia for this problem? How can I do? Full code here
Why don't you make some function which will take node field as parameter and return it's value
function field_value($field){
return $field['und'][0]['value'];
}
Something like that (not tested).
But if you don't want to use function try using curly braces like:
$location = $load->{field_testimonial_location.$und_value};
That should work...
Extending answer posted by MilanG, to make function more generic
function field_value($field, $index = 0 ){
return $field['und'][$index]['value'];
}
There are time when you have multi value fields, in that case you have to pass index of the value also. For example
$field['und'][3]['value'];
Please do not use such abbreviations, they will not suit all cases and eventually break your code.
Instead, there is already a tool do create custom code with easier syntax: Entity Metadata Wrapper.
Basically, instead of
$node = node_load($nid);
$field_value = $node->field_name['und'][0]['value'];
you can then do something like
$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
$field_value = $node_wrapper->field_name->value();
With the node wrapper you can also set values of a node, it's way easier and even works in multilingual environments, no need to get the language first ($node->language) or use constants (LANGUAGE_NONE).
In my custom module, I often use $node for the node object and $enode for the wrapper object. It's equally short and still know which object I am working on.
Related
I'm quite confused about this behavior in PHP and I'm not sure, how to solve it.
I want to do this:
I generate Object and use some methods to set its attributes. Then I want to "cache" the object, so I will store it into other variable and then I do something else with the object but it also affects cached object.
Could you give me some advice, how to do this?
Here is code snippet:
$query = new Obj();
$this->item->generateItemsQuery($query);
$this->itemsQuery = $query; // here I "cache" the variable for next usage...
// here I edit the old variable $query
if ($this->getFilter('limit') !== null) {
$query = $query->limit($this->getFilter('limit'));
}
if ($this->getFilter('page') !== null) {
$offset = ($this->getFilter('page') - 1) * $this->getFilter('limit');
$query = $query->offset($offset);
}
public function generateItemsQuery(&$query)
{
// some other things like this: $query = $query->offset($offset);
}
In this example -> problem is, that when I apply method "limit" and "offset" on $query it also affect $this->itemsQuery
Could you provide me some solution?
Thank you very much
You may want to take a look at:
http://php.net/manual/en/language.oop5.references.php
Specifically:
an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object.
If you want to create a clone of the object you will need to do:
$this->itemsQuery = clone $query;
See: http://php.net/manual/en/language.oop5.cloning.php
I try to dynamically retrieve a method of a class but php throws an exception which says Undefined property: stdClass ...
and How i try to get the values
private function getExactValue($row, $name)
{
$tempRow = clone $row;
foreach( explode('->', $name) as $key => $value)
{
$temp = $tempRow->{$value};
unset($tempRow);
$tempRow = $temp;
}
return $tempRow;
}
$row is an instance of an Object (not Std one)
$name is what i need in the Object to traverse , for example when i need $row->student->gifts->totalPoint() just pass the student->gifts->totalPoint() to the method for $name parameter
can you tell me what my mistake is?
I see what you are trying to do here. My first word of advice is that you are going about what you are trying to achieve in a very hackish way. If you wanted a better way to be able to execute arbitrary methods on an unknown object, I would suggest you look into PHP's reflection capabilities.
That being said, the problem with your code would appear to be that you are trying to execute a method via string, where what you need to do is utilize the method's name. What I would suggest is that within your loop where you explode the string on ->, you try to detect if it is a method or not, and then act accordingly. That could look like this:
foreach( explode('->', $name) as $value)
{
$value_trimmed = rtrim($value, '()');
if ($value === $value_trimmed) {
// this is a property
$tempRow = $tempRow->{$value};
} else {
// this is a method
$tempRow = $tempRow->{$value_trimmed}();
}
}
You should probably also do some validation on the input as well to make sure you have valid property/method names for each segment, as well as add validation that the entire string is indeed properly formed (i.e. you don't have things like foo->->bar(())). Of course this make no mention of how to handle array like foo[0]->bar() which you might also need to accommodate.
I wana to assign a variable such as 'heloo' to an address such as ->system_settings->settings->hostname and i write a function for.now when i write that address manually this function work correctly and assign 'hello' to that address,but,when i wana to gave address dynamically it doesn't work.
my function :
<?php
write_xml("->system_settings->settings->hostname",'Helloooooooo');
function write_xml($tag_address,$value) {
$xml = simplexml_load_file("test.xml")
or die("Error: Cannot create object");
// $xml->system_settings->settings->hostname = $value;
$xml->$tag_address=$value;
$xml->asXML("test.xml");
}
?>
when i run the command line it works but in dynamical mode it doesn't work and identifies $tag_address in this line $xml->$tag_address=$value; as a string,not as an address.
what should i do?
TNX
The solution is not that easy.
The easiest, but least secure, is to use eval() function so you can write something like this:
eval('$xml'.$tag_address.' = $value;'); // note ' as quotation marks
The most correct way can be that you split your text and create a chained object manually. You can't just refer to all chained elements in one string, but you can do this step-by-step with one element.
For example something like
$splitted_text = explode('->', $tag_address);
$node = $xml;
foreach($splitted_text as $object)
$node = &$node -> {$object};
// at the moment $node = $xml->system_settings->settings->hostname
$node = $value;
$xml->asXML("test.xml");
should work. I haven't tested it, but the idea is that in each iteration you prepare $node variable going deeper into the $xml variable. At the end you modify only the $node, but as objects are only references, so $xml should change accordingly.
This example assumes that there is no -> in the beginning of $tag_address. If it is, there would be a problem with explode() function because this would create empty string as the first element of the $splitted_text array.
So you might need to remove this empty element or apply calling as
write_xml("system_settings->settings->hostname",'Helloooooooo');
without the first ->.
Use XPath to select the node, then update the value. For that, you need the proper systax for your tag address.
write_xml("system_settings/settings/hostname", 'Helloooooooo');
function write_xml($tag_address, $value)
{
$xml = simplexml_load_file('test.xml') or die("Error: Cannot create object");
$nodes = $xml->xpath($tag_address);
$nodes[0][0] = $value;
$xml->asXML('test.xml');
}
The problem is easy to explain. What I want to do is to create an object, parameters and function calls based on $_POST variables.
$obj = new $_POST['object']['name']($_POST['object']['params']);
return json_encode(
$obj->$_POST['function']['name']($_POST['function']['params'])
);
The only problem here is that it gives the parameters as an array. And I want it to seperate the variables with a comma, like: $obj->function($var1 , $var2, $var3);. How can I achieve this?
This can be done, but you should only do it using a white list of possible objects to create. Otherwise it is kind of dangerous. A system this open may not be a good idea. If you can rethink the problem it may help to approach it a different way.
To solve your problem though, you can use a construct like this:
// Possible values:
$acceptable_classes = array('myClass1', 'myClass2', 'myClass3');
// If it is an allowed class,
if (in_array($_POST['object']['name'], $acceptable_classes) {
// Store it in a variable.
$class = $_POST['object']['name'];
$obj = new $class;
}
You need to keep a similar whitelist for the function. You might use a multidimensional array for that. You may extend this to hold acceptable parameters for the possible functions. It can blow out of proportion pretty quickly, but will help maintain security.
$acceptable_classes = array(
'myClass1' => array(
'func1', 'func2'
),
'myClass2' => array(
'func3', 'func4'
)
);
if ($acceptable_classes, array_key_exists($_POST['object']['name']) {
if (in_array($_POST['function']['name'], $acceptable_classes($_POST['object']['name'])) {
// Store it in a variable.
$class = $_POST['object']['name'];
$obj = new $class;
$func = $_POST['function']['name'];
return json_encode($obj->$func($_POST['function']['params']));
}
}
Do something like this:
call_user_func_array( array($obj, $_POST['function']['name']), $_POST['function']['params'])
Documentation of the call_user_func_array function
call_user_func_array() is what you're looking for. On a side note, running $_POST values is a security risk, I hope you sanitize them properly.
The best way is to create object without arguments passed to constructor and initiate it using some other method. For example:
$obj = new $_POST['object']['name']();
$result = call_user_func_array(array($obj,'init'),$_POST['object']['params']);
Also, in PHP 5.4+ you can use this "hack":
$reflection = new ReflectionClass($_POST['object']['name']);
$obj = $reflection->newInstanceWithoutConstructor();
call_user_func_array(array($obj,'__constructor'),$_POST['object']['params']);
But I advise you to change your architecture so you would be able to pass array to constructor "as is".
have the function take an array
$name = $_POST['object']['name'];
$obj = new $name;
$array = explode(',', $_POST['object']['params'];
$obj->function($array);
I want to trigger a function based on a variable.
function sound_dog() { return 'woof'; }
function sound_cow() { return 'moo'; }
$animal = 'cow';
print sound_{$animal}(); *
The * line is the line that's not correct.
I've done this before, but I can't find it. I'm aware of the potential security problems, etc.
Anyone? Many thanks.
You can do that, but not without interpolating the string first:
$animfunc = 'sound_' . $animal;
print $animfunc();
Or, skip the temporary variable with call_user_func():
call_user_func('sound_' . $animal);
You can do it like this:
$animal = 'cow';
$sounder = "sound_$animal";
print ${sounder}();
However, a much better way would be to use an array:
$sounds = array('dog' => sound_dog, 'cow' => sound_cow);
$animal = 'cow';
print $sounds[$animal]();
One of the advantages of the array method is that when you come back to your code six months later and wonder "gee, where is this sound_cow function used?" you can answer that question with a simple text search instead of having to follow all the logic that creates variable function names on the fly.
http://php.net/manual/en/functions.variable-functions.php
To do your example, you'd do
$animal_function = "sound_$animal";
$animal_function();
You can use curly brackets to build your function name. Not sure of backwards compatibility, but at least PHP 7+ can do it.
Here is my code when using Carbon to add or subtract time based on user chosen type (of 'add' or 'sub'):
$type = $this->date->calculation_type; // 'add' or 'sub'
$result = $this->contactFields[$this->date->{'base_date_field'}]
->{$type.'Years'}( $this->date->{'calculation_years'} )
->{$type.'Months'}( $this->date->{'calculation_months'} )
->{$type.'Weeks'}( $this->date->{'calculation_weeks'} )
->{$type.'Days'}( $this->date->{'calculation_days'} );
The important part here is the {$type.'someString'} sections. This will generate the function name before executing it. So in the first case if the user has chosen 'add', {$type.'Years'} becomes addYears.
For PHP >= 7 you can use this way:
function sound_dog() { return 'woof'; }
function sound_cow() { return 'moo'; }
$animal = 'cow';
print ("sound_$animal")();
You should ask yourself why you need to be doing this, perhaps you need to refactor your code to something like the following:
function animal_sound($type){
$animals=array();
$animals['dog'] = "woof";
$animals['cow'] = "moo";
return $animals[$type];
}
$animal = "cow";
print animal_sound($animal);
You can use $this-> and self:: for class-functions. Example provided below with a function input-parameter.
$var = 'some_class_function';
call_user_func(array($this, $var), $inputValue);
// equivalent to: $this->some_class_function($inputValue);
And yet another solution to what I like to call the dog-cow problem. This will spare a lot of superfluous function names and definitions and is perfect PHP syntax and probably future proof:
$animal = 'cow';
$sounds = [
'dog' => function() { return 'woof'; },
'cow' => function() { return 'moo'; }
];
print ($sounds[$animal])();
and looks a little bit less like trickery as the "string to function names" versions.
JavaScript devs might prefer this one for obvious reasons.
(tested on Windows, PHP 7.4.0 Apache 2.4)