How do I access the contents of this variable in PHP? - php

I am using a library, XCRUD, which takes a string argument and is expecting a variable interpolation pattern. Here is how it is used in the documentation, which works fine.
$xcrud->column_pattern('username','My name is {value}');
I want to use this variable as a key to an array, but I can't figure out what syntax is required to access it.
I have tried the following:
$xcrud->column_pattern('PlanNo', $myArray['{value}']);
$xcrud->column_pattern('PlanNo', $myArray[eval('{value}')]);
$xcrud->column_pattern('PlanNo', $myArray[${value}]);
How is it that the function in the library I'm calling can access the variable through {}? Maybe it's unreasonable for me to expect it will exist in the current scope, and it just passes that string somewhere down the line.
Thanks for your help. :)

Try this
$xcrud->column_pattern('PlanNo', $myArray[eval("(" + value + ")")]);

Related

1st argument in array_push only accepts variable

array_push
I have a PHP statement as follow (in a method of a class)
array_push(self::USER_BASIC_DETAIL_FIELDS, 'cname_username');
which gives me error
Cannot pass parameter 1 by reference
Then I tried it assigning it to variable and it all worked fine
$r = self::USER_BASIC_DETAIL_FIELDS;
array_push($r, 'cname_username');
My question is why does PHP throws an error in above case?
I have an answer but I am not sure so asked here. The answer is like:
array_push does not return the modified array but changes the variable given at argument 1. So change are made at the locations in memory where variable (argument 1) is stored.
If we are passing argument 1 as self::USER_BASIC_DETAIL_FIELDS then with the same behavior of array_push it will try to modify constant USER_BASIC_DETAIL_FIELDS of a class which will create mess for developer
Am I right?
The answer is: everything depends on the details of your project.
You can't modify the value of constants.
And the '$r' variable is not a pointer to 'self::USER_BASIC_DETAIL_FIELDS' it is a copy of 'self::USER_BASIC_DETAIL_FIELDS'.
I think than you need a static variable instead of constant in that case.

PHP operator '->' $foo-> {''}

I noticed that on php with the JSON lib we can access an element with a syntax I haven't seen before.
$jsonObject->{'myvar'}; // return the value of myvar on the jsonobject
What this statement mean?
I do not think it is inherent with the JSON lib.
It's just another way to determine the name of the variable you want to call. For example, it's useful when you want to decide dynamically which variable should be called, something that is of great use in magic methods.
$var->{"var_name"} is the equivalent of $var->var_name.
You can do things like these as well:
$key = "key_name";
var_dump($object->{$key});
You can also access keys with invalid characters such as dots or commas.
$key = "key.name.nice";
var_dump($object->{$key});

What is the proper name for accessing an object property by string?

I've been searching for quite a while and cannot find what this method is actually called.
In PHP example:
$var->{'property_name'}
Depending on what you are accessing it will be called...
A variable variable
A variable property
A variable function
It is worth noting that the curly-braces are only needed when you need to disambiguate an expression (bear in mind the string you use may itself be stored in a variable!)
And so on. This is documented in the PHP manual for variable variables.

Error in syntax using php

i have this line here.. it gives me an error..
Could you please take a look at this?
Thanks
$slideshow-auto2=$this->params->get("slideshow-auto2");
Invalid variable name:
$slideshow-auto2=$this->params->get("slideshow-auto2");
^---can't have this in a var name.
You're trying to do (from PHP's view), $slideshow minus constant "auto2" equals ...
I think you're missing a >:
$slideshow->auto2=$this->params->get("slideshow-auto2");
// ^ Right here
$slideshow-auto2 is not a valid variable name. You can't have hyphens in a variable name (PHP sees it as a minus).
Most of the other answers are guessing that you intended to use the -> syntax. If $slideshow is an object and auto2 is a property of that object, then this is what you want.
However, given the context of the rest of your line of code, my guess is that you want to have an actual variable named $slideshow-auto2. Unfortunately, this just isn't allowed. You'll need to work around it. You could name your variable $slideshowAuto2 or $slideshow_auto2 or various other alternatives, but not $slideshow-auto2.
You're trying to substract a property from an object, I guess you want to access that property so add a '>'
$slideshow->auto2=$this->params->get("slideshow-auto2");
Are you trying to use a hyphen within a variable name? That won't work because it's being interpreted as a minus sign and subtracting a property from an object does not work. You probably want something like this instead:
$slideshow->auto2=$this->params->get("slideshow-auto2");
Edit:
If you don't intend to access the property 'auto2', simply replace the hyphen with a valid character for a variable name.

Can I extract function return value?

I noticed that in PHP extract(some_function()); will work just like:
$stuff = some_function();
extract($stuff);
But in the PHP's documentation the extract function argument has the & thingy in front, and from what I know that means you have to pass a variable to it.
If the documentation was right, this would produce a strict standards message:
PHP Strict standards: Only variables should be passed by reference
So I think you just found a bug in the documentation. Congratulations.
EDIT
It still doesn't complain if you use it with EXTR_REFS as a second argument:
~❯ php -a
Interactive shell
php > function a(){return array('pwet'=> 42);}
php > extract(a(), EXTR_REFS);
php > echo $pwet;
42
Which is strange because referencing variables defined inside a function doesn't make much sense to me. I think the & might have been introduced because of this option, but appears only in the doc and is not enforced in the code.
EDIT
It seems I'm right, I found this comment in ext/standard/array.c (branches 5.3 and 5.4):
/* var_array is passed by ref for the needs of EXTR_REFS (needs to
* work on the original array to create refs to its members)
* simulate pass_by_value if EXTR_REFS is not used */
The ampersand passes a variable by reference so that when it is used in a function, you are manipulating the original object -- not a new variable with the same value. The documentation is telling you that if you pass a variable to the extract function, then the original object can be updated in some fashion by that function.
So, the answer is yes, you need to pass a variable to that function.
The reason $var_array parameter of the extract function is passed by reference (most likely) is from a holdover from older versions of PHP. Newer versions automatically pass arrays by reference.
The extract function creates a variable list from the contents of a (potentially large) array and it is not recommended that data of that type be passed by value.
Long story short, assign your array to a variable and pass it in that way.

Categories