<?php
class obj {
var $primary_key;
function obj($key = null){
if(isset($key)){
$this->primary_key = $key;
}
echo "primary_key: ".$this->primary_key."<br/>";
$this->obj_id = 14;
echo "obj_id: ".$this->obj_id."<br/>";
$key = $this->primary_key;
echo "obj_id from primary_key string: ".$this->$key."<br/>";
}
}
?>
Is there a way to get $this->primary_key value?
Like $this->$this->primary_key?
<?php
class obj {
var $primary_key;
function obj($key = null){
if(isset($key)){
$this->primary_key = $key;
}
echo "primary_key: ".$this->primary_key."<br/>";
$this->obj_id = 14;
echo "obj_id: ".$this->obj_id."<br/>";
$key = $this->primary_key;
echo "obj_id from primary_key string: ".$this->$key."<br/>";
}
}
?>
$aaa = new obj("obj_id");
i want to get $this->obj_id value, but i dont whant to use variable $key.
This line $this->obj_id = 14; will be dynamic and i would't be able to know,
if $this->obj_id will be named obj_id or something else like $this->banana, it would be desided by new obj("banana")
Well in order for that function to even be of any use, you would need to create a new class and set it to a variable, such as this:
$myclass = new obj(1);
Although that form of a constructor is only supported for backward compatibility (deprecated, you're really supposed to use __construct now), it should still run and set all your variables, which can be retrieved in the following way:
echo $myclass->primary_key; // the primary_key value
echo $myclass->obj_id; // the obj_id value
Edit: Also this line needs to be corrected to change the '$key' to 'primary_key' near the end. Putting '$key' there will not return anything because it would be sending '$this->1' which does not exist.
echo "obj_id from primary_key string: ".$this->primary_key."<br/>";
$obj_a = new obj(5);
echo $obj_a->primary_key; // prints '5'
In your last line:
echo "obj_id from primary_key string: ".$key."<br/>";
or:
echo "obj_id from primary_key string: ".$this->$primary_key."<br/>";
Seriously, I still don't understand the utility of this code...
Related
function report_test() {
$color = "blue";
$name = "John";
mobile_test($color, $name);
echo $columnid;
}
function mobile_test($color, $name) {
-snipped Insert MySQL Query-
$columnid = 5;
return $columnid;
}
Hopefully the example above portrays what I'm attempting to do. Globalising the variable columnid isn't an option in this scenario due to other various reasons. I also do not want to alter the mobile_test($color, $name) function. I find that if I echo the function instead the MySQL Insert Query is ran twice, meaning two sets of the same results gets entered into the database.
Is there another way to do this?
I assume that you want to echo out the return value from mobile_test() inside of report_test(). The easiest way to do this is to simply echo out $columnid inisde of mobile_test(). When you call mobile_test() from report_test(), the echo statement will be called as well, and the value will be outputted:
function mobile_test($color, $name) {
...
$columnid = 5;
echo $columnid;
return $columnid;
}
There's also the possibility to make use of PHP's short tag syntax (as <?= mobile_test(); ?>), assuming you want to echo the return of the function directly, and not do anything else with it.
Note that if you leave in the return value (return $columnid;), you'll be able to use this value directly as a comparison in the report_test() function:
if (mobile_test($color, $name) === 5) {
echo "The column ID is 5" /* This line will be triggered */
}
You may do this:
function report_test() {
$color = "blue";
$name = "John";
$columnid = mobile_test($color, $name);
echo $columnid;
}
you need to store the returned value from mobile_test() as a new variable. This won't need to change the mobile_test()
So I have this array:
$myArray = array('myVar', 'myVar75', 'myVar666');
How do I isset check whether the variable called $myVar,$myVar75,$myVar666 exists or not?
What is the most sensible way of passing the array's value into the isset() function as a variable name to check?
Just use variable variables to test each element with a simple foreach loop:
Example:
$myVar666 = 1; // for example's sake
$myArray = array('myVar', 'myVar75', 'myVar666');
foreach($myArray as $element) {
if(isset(${$element})) {
echo $element, ' is already set';
} else {
echo $element, ' is not yet set';
// if not set, do something here
}
}
Should yield something like this:
$myVar is not yet set
$myVar75 is not yet set
$myVar666 is already set
foreach($myArray as $value){
if(isset($$value)){
echo "$value is exist with value : '".$$value."'";
}
else{
"$value is not exist";
}
}
You'd do that by putting an extra $, as follows:
$myArray = array("myVar", "myVar75", "myVar666");
if (isset($$myArray[0])) {
//Content
}
That would check if $myVar is set. Change the index to myArray accordingly.
The way to think about PHP's implementation of this (called variable variables) is that the content of the string goes after the dollar sign:
$hi = "hello";
But if you put $$hi, then the $hi is replaced with the string's content, so it becomes $hello.
$hi = "hello";
$hello = "greetings";
If you put $$$hi, then the $hi is replaced so that it becomes $$hello, and the $hello is replaced so that it becomes $greetings.
I have just written a program to check for the variable scopes in PHP. The code goes like this:
<?php
$value = 1;
function change_value(){
if(some_condition){
$value = 0;
$asset = 1;
}else{
$asset = 0;
}
return $asset;
}
echo $value;
change_value();
echo $value;
?>
Now, the output of the above program is 11.
How can I change the value of $value once it enters the function change_value() ?
Pass the parameter by reference:
<?php
$value = 1;
function change_value(&$value){
if(/* some_condition */){
$value = 0;
$asset = 1;
}else{
$asset = 0;
}
return $asset;
}
echo $value; // echoes 1
$asset = change_value($value);
echo $value; // echoes 0
echo $asset; // echoes 0 or 1 depending on /* some_condition */
?>
Please don't use global ... even if some suggest it.
It's bad. It let's the variable be accessable from all over the script and you will be very confused when you come upon the situation where you access $value in a different script you included and it acts different then...
What you're trying to do, goes against common principles, but.
$GLOBALS['value'] = 0;
Using this inside function will let you change that value, but I suggest you not to do this.
The way others have outlined, is far more right.
Is there a way to access a member of an object, by using its name as a string?
When I declare an array...
$array = array();
$array['description_en']="hello";
$array['description_fr']="bonjour";
then I access a member like this:
$lang="en"; //just to show my purpose. it will be dynamic
$description = $array['description_'.$lang];
Can I do the same thing for objects?
For example:
$obj->description_en="hello";
$obj->description_fr="bonjour";
How can I access $obj->description_.$lang ?
class test
{
public $description_en = 'english';
}
$obj = new test();
$lang = 'en';
echo $obj->{"description_".$lang}; // echo's "english"
You can see more examples of variable variables here.
You can use this syntax:
<?php
class MyClass {
public $varA = 11;
public $varB = 22;
public $varC = 33;
}
$myObj = new MyClass();
echo $myObj->{"varA"} . "<br>";
echo $myObj->{"varB"} . "<br>";
echo $myObj->{"varC"} . "<br>";
This way, you can access object variables as if they were entries in an associative array.
I read php document and I saw this:
class foo{
var $bar = 'I am a bar';
}
$foo = new foo();
$identity = 'bar';
echo "{$foo->$identity}";
And I saw somebody wrote like this:
if (!isset($ns->job_{$this->id})){
//do something
}
But when I tried with this code, It didn't work:
$id1 = 10;
$no = 1;
echo ${id.$no};
Can you guys tell me why it didn't work and when I can use braces with variable correctly?
Live example
Brackets can be used on object types, for instance, to simulate a array index. Supposing that $arr is an array type and $obj an object, we have:
$arr['index'] ===
$obj->{'index'}
You can make it more fun, for instance:
$arr["index{$id}"] ===
$obj->{"index{$id}"}
Even more:
$arr[count($list)] ===
$obj->{count($list)}
Edit: Your problem --
variable of variable
// Your problem
$id1 = 10;
$no = 1;
$full = "id{$no}";
var_dump($$full); // yeap! $$ instead of $
What are you expecting?
$id = 10;
$no = 1;
echo "${id}.${no}"; // prints "10.1"