PHP object property from class variable? [duplicate] - php

This question already has answers here:
PHP Object Variable variables name?
(5 answers)
Closed 9 years ago.
Why does this work
foreach ($items as $i) {
$dataTitle = $this->dataTitle;
$title = $i->$dataTitle;
}
when this doesn't?
foreach ($items as $i) {
$title = $i->$this->dataTitle;
}
Is there a better way to do this?

Try this:
$title = $i->{$this->dataTitle};
Your expression is being parsed as:
$title = ($i->$this)->dataTitle;

$this referes to current object parsed in not obvious order. You need to use {expr} notation, to dynamicly evaluate property name.
Try to use {} around $this->dataTitle:
$title = $i->{$this->dataTitle};
Look at bottom part of last example in variable variables section of manual.

Related

PHP's For-each / for behaviour - Value remains accesible after Foreach ends [duplicate]

This question already has answers here:
PHP, an odd variable scope?
(6 answers)
Closed 11 months ago.
I have this piece of code:
$exists = false;
foreach ($outsiderTests as $key => $outsiderTest) {
$textExists = null;
foreach ($tests as $test) {
if ($outsiderTest->getName() == $test->getName()) {
$exists = true;
$existingTest = $test;
break;
} else
$exists = false;
}
var_dump($existingTest, $test);
}
As you can see, I want to see if there is an equivalent to an outsiderTest in $tests array. I thought I would have to save the existing equivalent $test on another variable as it would be gone after the foreach ends, but it does not.
The value of $existingTest and $test is the same when I dump them. This is cool, and makes me able to get rid of the mentioned $existingTest variable, but makes me wonder if I am understanding PHP's loop functionality.
Doesn't the $test variable only exist inside the foreach scope? Does PHP temporarily save the value of the last index the execution has run through?
PHP's variable scope is explained here: https://www.php.net/manual/en/language.variables.scope.php
Effectively you have 2 scopes:
The global scope
The local function scope
So a loop variable will be accessible out of it's scope and will contain the last value it had. This is why you got this behaviour.
If you have a loop calling a function then you have multiple options:
Declare the external variable with the global keyword inside the function.
Access globals with the $GLOBALS variable.
Pass the globals you need to your anonymous function with the use () syntax.

PHP - auto assign a different name to a variable of variable [duplicate]

This question already has answers here:
Using braces with dynamic variable names in PHP
(9 answers)
Closed 6 years ago.
Using PHP variable variables with mysqli_fetch_assoc to auto-format variables like $column_name="some_value" (code below):
while ($account_row=mysqli_fetch_assoc($account_results))
{
foreach ($account_row as $key=>$value)
{
$$key=trim(stripslashes($value));
}
}
So if I have a column "username" and row value "someuser", this code creates:
$username="someuser";
However, in some cases I need variable names to be DIFFERENT from column names. For example, I need code to create:
$username_temp="someuser";
How could I do that? Using this code gives an error:
$$key."_temp"=trim(stripslashes($value));
No other ideas in my head.
change $$key."_temp" to ${$key."_temp"} have a look on below solution:
$value = 'test';
$key="someuser";
${$key."_temp"}=$value;
echo $someuser_temp; //output test
Please try this ${$key . "_temp"} = trim(stripslashes($value));

Problems with syntax in PHP [duplicate]

This question already has answers here:
How to concatenate PHP variable name?
(4 answers)
Closed 6 years ago.
I have an object and can access one of its properties by
$book = new Book(); print $book->price;
I can also call
$prop = "price"; print $book->$prop.
But I cannot figure out how to call in a mixed way, it gives error:
$book->id_$prod
as a shortened of $book->id_products, being $prod = "products".
$book = new Book();
$id_prod = "id_$prod";
print $book->$id_prod;
Mix the 2 first options.
$prop = 'id_'.$prod;
$book->$prop;

Assigning value to dynamic object's property [duplicate]

This question already has answers here:
How do I dynamically write a PHP object property name?
(5 answers)
Closed 7 years ago.
This is not only tricky to explain, but tricky to do:
I'm trying to access and replace
$myObject->customField[0] = "some value";
but if I do
$str = "customField";
$myObject->$str[0] = "some value";
That doesn't work and if I do
$str = "customField";
$obj = $myObject->$str;
$obj[0];
That won't work either. I can change the values if I don't do this dynamically but I'm having to loop through a lot so doing it dynamic will be very helpful.
EDIT (answer)
Turns out curly braces does the trick. ie
$str = "customField";
$myObject->{$str}[0] = "some value";
Why do you want to have dynamic property names? The best answer is: don't do it this way. Consider using an associative array instead:
$myObject->customFields = array();
$myObject->customFields[$str] = "some value";

Retrieve POST values in PHP using a dynamic method [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 8 years ago.
I was wondering if it's possible to do something like:
for ($d=0; $d<20; $d++) {
$productname.$d = $_POST['productname'.$d];
$link.$d = $_POST['link'.$d];
$color.$d = $_POST['color'.$d];
$size.$d = $_POST['size'.$d];
$otherinfo.$d = $_POST['otherinfo'.$d];
$no.$d = $_POST['no'.$d];
$other.$d = $_POST['other'.$d];
}
since the code above doesn't work.
What am I doing wrong? Any help is appreciated.
Your $productname.$d statement has no sense.
Use $productname[$d] instead. It is array approach and it is much more preferable.
P.S.: If you really want so many different variables you can use variable variables (pseudocode is below):
$varName = 'productname'.$d;
$$varName = $_POST['productname'.$d];;

Categories