Problems with syntax in PHP [duplicate] - php

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;

Related

There is option to get expected type of variable in PHP? [duplicate]

This question already has an answer here:
How to get the string name of the argument's type hint?
(1 answer)
Closed 2 years ago.
I have method which cast array to object by using
$class = get_class($object);
$methodList = get_class_methods($class);
But now I need had information about expected type of variable too. For example from this method:
public function setFoo(int $foo)
{
}
I need get int too. There is any option to get it?
You can use Reflection. Specifically ReflectionParameter::getType().
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
assert($reflectionType1 instanceof ReflectionNamedType);
echo $reflectionType1->getName(), PHP_EOL;
var_dump($reflectionType2);
The above example will output:
int
NULL

Only variables should be passed by reference Strict standards on line 138 [duplicate]

This question already has answers here:
Only variables should be passed by reference
(12 answers)
Closed 5 years ago.
usort($qus_with_ans, "qus_sort");
$ary_val = max(array_column($qus_with_ans, 'updated_at'));
$ary_key = array_search($ary_val, array_column($qus_with_ans, 'updated_at'));
$k = $qus_with_ans[$ary_key];
$curnt_sub_id = $k['subject_id'];
$curnt_sub_name = $k['s_name'];
$last_question_key = end(array_keys($qus_with_ans));
We have a error Strict standards: Only variables should be passed by reference on last line of code i can't understand why error comes Please fix my issue
line no. 138 are $last_question_key = end(array_keys($qus_with_ans));
You can't use function return in end function and should convert it to the variable.
$keys=array_keys($qus_with_ans);
$last_question_key = end($keys);
However, use arrya_pop which is pushing-out last element from the array
$last_question_key = array_pop(array_keys($qus_with_ans));
If you anyway don't have intension to use keys elsewhere from performance point of view.

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

PHP object property from class variable? [duplicate]

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.

How can I call a class with variables from array php? [duplicate]

This question already has answers here:
Is there a call_user_func() equivalent to create a new class instance?
(2 answers)
Closed 8 years ago.
I would like something like this but dynamically from an array like this:
$array = array("first","second","third");
So class would be called like this:
$class = new class("first","second","third");
You can use reflection for it:
$refClass = new ReflectionClass('SomeClass');
$instance = $refClass->newInstanceArgs($args);

Categories