Get properties from string miltilevel - php

I want get value of laravel model my this
$key = "name";
$this->$key; //returns the name
but, i want get multilevel values, like this
$key = "role->name";
$this->$key; //returns nothing
what i can do?

It looks like you're trying to access the property of a property. Don't put both property names into the same variable. You can use two variables instead:
$first_level_property = "role";
$second_level_property = "name";
$this->$first_level_property->$second_level_property
More examples of how to access property values.

Related

how can i set value to an array like chaning methods?

i wanna set value with key to an arrau like chaning
I just want to know how Laravel did it
this is laravel code of eloquent update query
$variable=Model::find(3);
$variable->columnname="name";
$variable->save();
this is my code
$variable=["name"=>"Eric","email"=>"example#gmail.com"];
$variable->name="jack";
$variable->email="testest#gmail.com";
print_r($variable); or $query="update tblname ..."
It doesn't work and it give me error
how this system work
You are mixing some things up here I guess.
// Array syntax
$array = [];
$array['key'] = 'value'; // adding "value" to the array with the key "key"
$array['key'] = 'newValue'; // Values can be changed like this.
// Object syntax
$obj = new stdClass;
$obj->key = 'value'; // adding "value to the object with as the attribute "key"
BUT this would just be for a standard Class. When you are using an already existing class (in your example "Model") you can not always access or change the attributes. That's why you can often see methods like
$obj->getName(); //to get the value of "name" of the class
$obj->setName(); // to set...
Getter and Setter? I did a quick search. I think the link should clear up a couple of things. And for your code:
// option 1, create a class with getter and setter, see link
// option 2
$variable = new stdClass;
$variable->name = "Jack";
...

composed variable after Object Operator in PHP

How can I build a composed variable while creating a variable in PHP?
(Sorry I'm not sure how to call the different elements)
This is what I'm trying to do:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->$language_.$i; // problem is here
}
I would like $language_.$i to be equivalent to name_english_1, next loop name_english_2... The same way I built $language
If you want to use an expression in a computed property, you have to put the expression in braces. Also, you need to put the underscore in quotes.
$data = $arraydata->{$language."_".$i};
However, I suggest you redesign your data structure. Instead of having separate name_LANG_i properties, make a single name property whose value is a multi-dimensional array.
$lang = $this->session->userdata('site_lang');
for ($i=1;$i<=3;$i++) {
$data = $arraydata->name[$lang][$i];
// do something with $data
}
Whenever you find yourself using variable variables or variable properties, it's almost always a sign that you should be using an array instead.
First construct the field name and then use it for accessing the field value from the object $arraydata. So your code should be like this:
$language = 'name_'.$this->session->userdata('site_lang');
for ($i = 1; $i <= 3; $i++) {
$var = "{$language}_{$i}";
$data = $arraydata->$var;
// echo $data;
}

Setting a value to variable

I have a problem assigning value to a variable in php.
Example what I am trying to do:
Suppose I have 3 variables and I want to assign the value of 3rd variable to 4th variable but in 3rd variable I want to use first 2 variables.
$depth = 1;
$forumcat = _cat;
$f1_cat = 'some html code';
$new = '';
Now I want to assign $f1_cat to $new variable
I know it can be done like $new = $f1_cat but I want to do it like
$new = $f$depth$forumcat;
I mean in place of using '1_cat' I want to use the variables.
I tried the following
$new = "\$f{$depth}{$forumcat}";
but this statement assigns a string $f1_cat but I want to assign $f1_cat variable value.
How to do it?
$new = ${'f'.$depth.$forumcat}; // This will contain value of $f1_cat
Also you may want to change $forumcat = _cat; to $forumcat = '_cat';. please note the single quotes added to _cat

PHP: set class array properties with a string as the propertyname

I have a class with a property that is an array:
class NewObject {
public $Props = array();
}
$obj = new NewObject();
$obj->Props[0] = 'a';
$obj->Props[1] = 'b';
Now I want to change the values of Props, not directly, but with a variable 'propertyname':
This DOES work for single string properties but not for arrays, because the key N is interpreted as the Nth letter of the STRING 'Props' instead of the Nth value in the array!
$propertyname = 'Props';
$obj->$propertyname[0] ='c'; //doesnt work as expected, it tries to set $obj->P now, it seems
$obj->$propertyname[1] ='d';
Any way to solve this ?
$obj->{$propertyname}[0] ='c';

Assigning PHP class fields to array values when passing both into a function doesn't set the fields, why?

As the description states, I have a function that takes in an array and an object as arguments and assigns all of the objects fields to their respective values in the array depending on the type of the object. The objects all have different fields, but they all have a type attribute which the function uses to determine which fields to assign.
It works something like this:
function unload($arr,&$obj){ <-- //&$obj not $obj
if($obj->type == 'A'){
echo 'Setting field for A';
$obj->a = $arr['a_value'];
//some more assignments..
}
elseif($obj->type == 'B'){
$obj->b = $arr['b_value'];
echo 'Setting field for B';
//some more assignments...
}
//some more elseifs
//return an error if
//object's type doesn't
//match
else{
echo 'Error: Object type '.$obj->type.' not recognized.';
}
}
$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';
$obj = new A(); //A's type set to 'A' upon initialization
unload($arr,$obj);
echo 'A->a set to: '.$obj->a;
Output:
A->a set to:
The code enters the correct branch for the object that is passed in but none of the object's fields get assigned. What am I doing wrong?
The server is running PHP 4.4.7, I still have no idea what's causing this.
Edit: I FINALLY figured it out, it was a combination of 2 things:
I didn't realize the $this keyword was required when referencing class field names from within the class. I assumed the variables had global scope so $this was optional like it is in Java. This is why just changing the function declaration didn't fix the problem. Now everything works fine!
Which PHP version are you on?
Because in PHP4 you need to explicitly pass the object by reference:
function unload($arr,&$obj){
If otherwise you are on PHP5, double check your $arr contents. And do some print_r inside and outside the function ...
If you want to get the class name i'd suggest you to use get_class() which will return the class name.
Anyway why are you using A->a instead of $obj->a? It seems to be wrong.
And notice that switch could best suits your needs in this case.
EDIT Finally got it: you have to replace
$arr['a'] = 'SomeValue';
$arr['b'] = 'SomeOtherValue';
with
$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';
or otherwise set $obj->b = $arr['a_value']; to $obj->b = $arr['a']; and do the same with the b value.
The meaning of this is that the array keys have to be the same.

Categories