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
Related
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.
Since objects are passed by reference by default now, is there maybe some special case when &$obj would make sense?
Objects use a different reference mechanism. &$object is more a reference of a reference. You can't really compare them both.
See Objects and references:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, 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. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
&$object is something else than $object. I'll give you an example:
foreach ($objects as $object) {
if ($cond) {
$object = new Object(); // This won't affect $objects
}
}
foreach ($objects as &$object) {
if ($cond) {
$object = new Object(); // This will affect $objects
}
}
I won't answer the question if it makes sense, or if there is a need. These are opinion based questions. You can definitely live without the & reference on objects, as you could without objects at all. The existence of two mechanisms is a consequence of PHP's backward compatibility.
There are situations where you add & in front of function name, to return any value as a reference.
To call those function we need to add & in front of object.
If we add & in front of object, then it will return value as reference otherwise it will only return a copy of that variable.
class Fruit() {
protected $intOrderNum = 10;
public function &getOrderNum() {
return $this->intOrderNum;
}
}
class Fruitbox() {
public function TestFruit() {
$objFruit = new Fruit();
echo "Check fruit order num : " . $objFruit->getOrderNum(); // 10
$intOrderNumber = $objFruit->getOrderNum();
$intOrderNumber++;
echo "Check fruit order num : " . $objFruit->getOrderNum(); // 10
$intOrderNumber = &$objFruit->getOrderNum();
$intOrderNumber++;
echo "Check fruit order num : " . $objFruit->getOrderNum(); // 11
}
}
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.
The main function of the example class uses the reusableFunction twice with different data and attempts to send that data to a different instance variable ($this->result1container and $this->result2container) in each case, but the data doesn't get into the instance variables.
I could get it to work by making reusableFunction into two different functions, one with array_push($this->result1container, $resultdata) and the other with array_push($this->result2container, $resultdata), but I am trying to find a solution that doesn't require me to duplicate the code.
My solution was to try to pass the name of the result container into the function, but no go. Does somebody know a way I could get this to work?
Example Code:
Class Example {
private $result1container = array();
private $result2container = array();
function __construct() {
;
}
function main($data1, $data2) {
$this->reusableFunction($data1, $this->result1container);
$this->reusableFunction($data2, $this->result2container);
}
function reusableFunction($data, $resultcontainer) {
$resultdata = $data + 17;
// PROBLEM HERE - $resultcontainer is apparently not equal to
// $this->result1container or $this->result2container when I
// try to pass them in through the parameter.
array_push($resultcontainer, $resultdata);
}
function getResults() {
return array(
"Container 1" => $this->result1container,
"Container 2" => $this->result2container);
}
}
(If this is a duplicate of a question, I apologize and will happily learn the answer from that question if somebody would be kind enough to point me there. My research didn't turn up any answers, but this might just be because I didn't know the right question to be searching for)
It looks to me like you want to be passing by reference:
function reusableFunction($data, &$resultcontainer) {
...
If you don't pass by reference with the & then you are just making a local copy of the variable inside reuseableFunction .
You are changing the copy, not the original. Alias the original Array by referenceDocs:
function reusableFunction($data, &$resultcontainer) {
# ^
And that should do the job. Alternatively, return the changed Array and assign it to the object member it belongs to (as for re-useability and to keep things apart if the real functionality is doing merely the push only).
Additionally
array_push($resultcontainer, $resultdata);
can be written as
$resultcontainer[] = $resultdata;
But that's just really FYI.
You may pass the attributes name as a String to the method like this:
function reusableFunction($data, $resultcontainer) {
$resultdata = $data + 17;
array_push($this->{$resultcontainer}, $resultdata);
}
//..somewhere else..
$this->reusableFunction($data, 'result2Container')
Some php experts wrote some texts about "why you shouldn't use byReference in php".
Another solution would be to define the containers as an array. Then you can pass an "key" to the method that is used to store the result in the array. Like this:
private $results = array();
function reusableFunction($data, $resIdx) {
$resultdata = $data + 17;
array_push($this->$results[$resIdx], $resultdata);
}
//..somewhere else..
$this->reusableFunction($data, 'result2Container');
//..or pass a number as index..
$this->reusableFunction($data, 1);
Suppose I have an array of a objects of user defined class. Wanted to know how do I extract the elements of the array in PHP.
// class definition
class User
{
public $fname;
public $lname;
}
// array of objects of the class defined above
$objUser1 = new User():
$objUser2 = new User():
$objUser3 = new User():
$objUser4 = new User():
$alUser = array();
$alUser[] = $objUser1;
$alUser[] = $objUser2;
$alUser[] = $objUser3;
$alUser[] = $objUser4;
// trying to iterate and extract values using typcasting - this does not work, what is the alternative.
foreach($alUser as $user)
{
$obj = (User) $user; // gives error - unexpected $user;
}
Thats how I used to do in java while extracting objects from the Java ArrayList, hence thought the PHP way might be similar. Can anyone explain it.
foreach ($alUser as $user) {
$obj = $user;
}
Why do you need typecasting for this?
PHP is a dynamically typed language. There is no need to cast in most cases.
It is impossible to cast to a User: see PHP's documentation on type juggling and casting.
This example would print "$user is a object (User)" four times.
foreach($alUser as $user) {
echo '$user is a ' . get_type($user);
if(is_object($user)) {
echo ' (' . get_class($user) . ')';
echo "\n";
}
It would be nice for example Eclipse PDT to determine the type of object for Code Completion. otherwise you are stuck backtracing, where the array was created and what objects were put into it and then look at the class file to see what functions are available (or temp create a new theObject() to see what methods/properties are available if you know what type of object it is. other times may not be as easy if many objects call functions that create those arrays and objects in them, so have to backtrace to see how those arrays made). Heard a few other IDE's may be able to determine type better like phpEd possibly?