Dynamic prefix attribute PHP [duplicate] - php

This question already has an answer here:
How can I access a property with an invalid name?
(1 answer)
Closed 6 years ago.
I have a question about attributes in PHP.
I have various Class with the same attributes, but with with different prefix.
Example:
$attr->a_field;
$attr2->b_field;
So, with another Class I want to access to them.
I tried:
$field = "{$prefix}_field";
$attr->{$field}
and it works perfect. But is any other way to doing this?
I tried also with:
$attr->{$prefix}_field;
$attr->{$prefix}{"_field"};
$attr->"{$prefix}_field";
etc and who I suppose I get PHP's errors
Thanks!

You can write it directly as $attr->{"{$prefix}_field"}, as shown in the docs.

You're looking into variable variables
$attr->{$prefix."_field"}

Related

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

Can't use method return value in write context PHP unset() [duplicate]

This question already has answers here:
Remove a child with a specific attribute, in SimpleXML for PHP
(18 answers)
Closed 7 years ago.
I know there are a lot of answers out there for this exact question, but none of them seem to help me solve my problem.
I have an xml file on my server, i need to use PHP SimpleXML to remove an element from the document. After some googling i found a number of answers saying to use unset() and then save the xml.
so i came up with this:
function deleteCourse($course){
$xml = self::getxml(); # get the XML file
unset($xml->xpath('course[name = "'.$course.'"]'));
$xml->asXml("data.xml");
}
now whenever i run this i get this error: PHP Fatal error: Can't use method return value in write context in blahblahLink on line 92
line 92 is unset($xml->xpath('course[name = "'.$course.'"]'));
I really hope somebody can help me out with this
unset won't work if you pass method return, pass variable content / array instead

PHP strings replace in a file [duplicate]

This question already has answers here:
Redefining constants in PHP
(5 answers)
Closed 8 years ago.
I have a php file like this.
define('TEXT_ONE', 'testvalue1');
define('TEXT_TWO', 'testvalue12');
define('TEXT_THREE', 'testvalue13');
define('TEXT_FOUR', 'testvalue14');
define('TEXT_FIVE', 'testvalue15');
define('TEXT_SIX', 'testvalue16');
define('TEXT_SEVEN', 'testvalue17');
define('TEXT_EIGHT', 'testvalue18');
define('TEXT_NINE', 'testvalue19');
define('TEXT_TEN', 'testvalue10);
define('TEXT_ELEVEN', 'testvalue11');
I want to change some of the defined value through php code.
for ex:- I Want to change above file to
define('TEXT_ONE', 'newtext1');
define('TEXT_TWO', 'newtext12');
define('TEXT_THREE', 'newtext13');
define('TEXT_FOUR', 'newtext14');
define('TEXT_FIVE', 'newtext15');
define('TEXT_SIX', 'newtext16');
define('TEXT_SEVEN', 'newtext17');
define('TEXT_EIGHT', 'newtext18');
define('TEXT_NINE', 'testvalue19');
define('TEXT_TEN', 'newtext10);
define('TEXT_ELEVEN', 'testvalue11');
Can any one help me?
Thanks
define — Defines a named constant.
As the name suggests, that value cannot change during the execution of the script

PHP: why are some internal functions missing from `get_defined_functions`? [duplicate]

This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx

How to use strings with variables in codeigniter's language files? [duplicate]

This question already has answers here:
Print string with a php variable in it
(4 answers)
Closed 11 months ago.
for a localized website I want to create different language files.
But my main problem before starting the localiuation is, that i probably have strings with variables.
My theory is that i can use placeholders within my language files like:
$lang['somekey'] = "Hello Mr. %s, how are you?";
Is there a clean and nice way to parse those variables or do i have to develop a function for that?
Thanks.
I have the same problem and do it simply by using,
echo sprintf($this->lang->line('somekey'),'XYZ');//load language library before use
Read sprintf()
you can use codeigniter i18n with PHP .sprintf() to achieve what you want. load up the codeigniter non-variable strings (with those format stuff), then pass it on to .sprintf() for formatting and assignment of values. it should replace the %s part.
it's similar to this question. .sprintf() works like .printf(), only that it returns the string rather than printing it.

Categories