I am porting C# code to PHP.
However, it seems like PHP ignores the array references. The code below gives out the last element 3 times. How can I fix this?
class Participants
{
public $name;
public $country;
public $town;
}
class Test
{
public function __construct()
{
$this->List = array(new Participants());
}
public function test()
{
$p = new Participants();
$p->name = "Harry";
$p->town = "Washington";
$p->country = "USA";
$List[0] = $p;
$p->name = "Janette";
$p->town = "Amsterdam";
$p->country = "Netherlands";
$List[1] = $p;
$p->name = "Piotr";
$p->town = "Moscow";
$p->country = "Russia";
$List[2] = $p;
echo $List[0]->name;
echo $List[1]->name;
echo $List[2]->name;
// this unfortunately ignores the index and echoes 3 times the last element.
}
}//end Test Class
Arrays in PHP are actually ordered hashmaps and they are indexable. Your problem has nothing to do with "indexes being ignored". Your problem is that $List[0], $List[1] and $List[2] are all references to the same object.
Objects are mutable, you only make 1 unique object and change the name/town/country 3 times.
When you assign $p to $List[0] you do not make a copy of the object, but instead give $List[0] a reference to the same object as $p, which you then proceed to alter.
If you want 3 objects you need to make 3 objects:
$p = new Participants();
$p->name = "Harry";
$p->town = "Washington";
$p->country = "USA";
$List[0] = $p;
$p = new Participants();
$p->name = "Janette";
$p->town = "Amsterdam";
$p->country = "Netherlands";
$List[1] = $p;
$p = new Participants();
$p->name = "Piotr";
$p->town = "Moscow";
$p->country = "Russia";
$List[2] = $p;
This works the same in nearly every OOP language BTW, including C#. The difference is that in C# you can define a value type (struct or enumeration) which does make a full copy when assigning. My guess is that the C# Participants is a struct.
Related
In PHP we can do things like these:
Class Example {
...
}
$example = 'Example';
$object = new $example();
Or the use of variable variables:
$hour = 18;
$greets = array('Good morning','Good afternoon','Good evening');
$values = array(13,21,23);//people is sleeping at 23PM, so they don't greet.
$n = count($values);
$greet = 'greets';
for($i=0;$i<$n;$i++){
if($hour < $values[$i]){
echo 'hello, '.${$greet}[$i];
break;
}
}
And others..
I wonder if it would be possible to access directly to a specific index of a multidimensional array in a similar way. Something like:
$array = array(...); //multidimensional array.
$position = '[0][4][3]';
print_r($array$position);
Thanks in advance.
UPDATE
I'm so sorry because I finished my question in a wrong way.
I need to set the multimesional array and add a value. i.e:
$array$position = $data;
You could implement it yourself with a custom function:
function getValueFromMultiDimensionalArray( array $array, string $key )
{
$keys = explode('][', $key);
$value = $array;
foreach ($keys as $theKey) {
// remove the opening or closing bracket if present
$theKey = str_replace([ '[', ']' ], '', $theKey);
if (!isset($value[$theKey])) {
return null;
}
$value = $value[$theKey];
}
return $value;
}
You can define path as dot separated , check the following solution
function getValueByKey($a,$p){
$c = $a;
foreach(explode('.',$p) as $v){
if(!array_key_exists($v, $c)) return null;
$c = $c[$v];
}
return $c;
}
You can use this function as
$path = '1.2.3.0';
$indexValue = getValueByKey($array, $path);
Nope, this is not possible.
The only thing you can do is to implement ArrayAccess interface, which allows to access instances with [] operator. But you will have to define the logic yourself.
class MyClass implements ArrayAccess
{
...
}
$x = new MyClass([0=>[4=>[3=>'hello world']]]);
$position = '[0][4][3]';
echo $x[$position]; //hello world
So I have:
$first = "one";
$second = "two";
How can I get values of these variables in:
class TD
{
public $first;
public $second;
}
on the contrary, from here:
class TD
{
public $first = "one";
public $second = "two;
}
to here:
$... = ... ;
$... = ... ;
need to then use it in the query(for ex.):
SELECT * FROM table WHERE cell = $first
Try the following:
$td = new TD;
$first = $td->first;
$second = $td->second;
The above variable $td is an instance of the class TD and it is accessing the classes properties, which are in this case $first and $second. Which is done like this $td->first. Learn all about OOP in PHP.
you can also add a constructor and pass an array for multi params
class TD
{
public $find = [];
public function __construct($arr){
foreach($arr as $k=>$v){
$this->find[$k+1] = $v;
}
}
}
$array = ["one", "two", "tree"];
$td = new TD($array);
var_dump($td->find[1]);
var_dump($td->find[2]);
var_dump($td->find[3]);
I'm wondering if this was possible and I could not find a way to do it so I ask. How can I get the name of the variable where in a instance of a class is present.
Pseudo code:
class test{
public $my_var_name = '';
function __construct(){
//the object says: Humm I am wondering what's the variable name I am stored in?
$this->my_var_name = get_varname_of_current_object();
}
}
$instance1 = new test();
$instance2 = new test();
$boeh = new test();
echo $instance1->my_var_name . ' ';
echo $instance2->my_var_name . ' ';
echo $boeh->my_var_name . ' ';
The output would be like:
instance1 instance2 boeh
Why! Well I just wanna know its possible.
I have no idea why, but here you go.
<?php
class Foo {
public function getAssignedVariable() {
$hash = function($object) {
return spl_object_hash($object);
};
$self = $hash($this);
foreach ($GLOBALS as $key => $value) {
if ($value instanceof Foo && $self == $hash($value)) {
return $key;
}
}
}
}
$a = new Foo;
$b = new Foo;
echo '$' . $a->getAssignedVariable(), PHP_EOL; // $a
echo '$' . $b->getAssignedVariable(), PHP_EOL; // $b
I created this code trying to answer for How to get name of a initializer variable inside a class in PHP
But it is already closed and referenced to this question,
just another variant easy to read, and I hope I didn't break any basic concept oh php development:
class Example
{
public function someMethod()
{
$vars = $GLOBALS;
$vname = FALSE;
$ref = &$this;
foreach($vars as $key => $val) {
if( ($val) === ($ref)) {
$vname = $key;
break;
}
}
return $vname;
}
}
$abc= new Example;
$def= new Example;
echo $abc->someMethod();
echo $def->someMethod();
I can't find a good reason to do that.
Anyways, one way you can do (but again it has no use as far as i can imagine) this is by passing the instance name as a constructor's parameter, like this:
$my_instance = new test("my_instance");
I'm confused about the following output in this linked list
class ListNode{
public $next = NULL;
public $data = NULL;
public function __construct($data){
$this->data = $data;
}
}
class LinkedList{
private $firstNode = NULL;
private $lastNode = NULL;
public function insertFirst($data){
$link = new ListNode($data);
$link->next = $this->firstNode;
$this->firstNode = &$link;
if($this->lastNode == NULL){
$this->lastNode = &$link;
}
}
public function readList(){
while($this->firstNode != NULL){
echo $this->firstNode->data;
$this->firstNode = $this->firstNode->next;
}
}
public function assessList(){
$copy = $this->firstNode;
echo $copy->data;
echo $this->firstNode->data;
$copy->data = 'm';
echo $copy->data;
echo $this->firstNode->data;
}
}
$linkedList = new LinkedList();
$linkedList->insertFirst('c');
$linkedList->insertFirst('b');
$linkedList->insertFirst('a');
//$linkedList->readList(); //output a b c
$linkedList->assessList(); //outputs a a m m
I would expect the output to be a a m a. I thought $copy is just a copy of the value stored in $this->firstNode.
Isn't this line of code $copy = $this->firstNode an assignment by value? I would expect the output to be a a m m if it was an assignment by reference $copy = &$this->firstNode but not if it was an assignment by value.
Can someone please clarify?
EDIT (additional example)
public function assessList(){
$copy = $this->firstNode->data;
echo $copy. "<br/>";
echo $this->firstNode->data. "<br/>";
$copy = 'm';
echo $copy. "<br/>";
echo $this->firstNode->data. "<br/>";
}
This:
$copy = $this->firstNode;
Is not a copy of the object, it's a copy of the "pointer" to the original object, so when you modify it, you modify the underlying object. You need to use the clone keyword to get a true copy:
$copy = clone $this->firstNode;
From the PHP docs (emphasis mine):
When assigning an already created instance of a class to a new
variable, the new variable will access the same instance as the object
that was assigned.
You can see from this example that your code snippet now outputs:
aama
My goal is to echo the argument passed to a function. For example, how can this be done?
$contact_name = 'foo';
function do_something($some_argument){
// echo 'contact_name' .... How???
}
do_something($contact_name);
You can't. If you want to do that, you need to pass the names as well, e.g:
$contact_name = 'foo';
$contact_phone = '555-1234';
function do_something($args = array()) {
foreach ($args as $name => $value) {
echo "$name: $value<br />";
}
}
do_something(compact('contact_name', 'contact_phone'));
Straight off the PHP.net variables page:
<?php
function vname(&$var, $scope=false, $prefix='unique', $suffix='value')
{
if($scope) $vals = $scope;
else $vals = $GLOBALS;
$old = $var;
$var = $new = $prefix.rand().$suffix;
$vname = FALSE;
foreach($vals as $key => $val) {
if($val === $new) $vname = $key;
}
$var = $old;
return $vname;
}
?>
Not possible.
Variables are just means to address values or areas in the memory. You cannot get the variable name that’s value has been passed to a function.
Disclaimer: this will oonly work if you pass a variable to the function, not a value, and it only works when your not in a function or a class. So only the GLOBAL scope works :)
Good funct($var)
Bad funct(1)
You can do it actually contrary to popular believe ^_^. but it involves a few lookup tricks with the $GLOBALS variable.
you do it like so:
$variable_name = "some value, better if its unique";
function funct($var) {
foreach ($GLOBALS as $name => $value) {
if ($value == $var) {
echo $name; // will echo variable_name
break;
}
}
}
this method is not fool proof tho. Because if two variables have the same value, the function will get the name of the first one it finds. Not the one you want :P
Its best to make the variable value unique before hand if you want accuracy on variable names
Another method would be to use reference to be accurate like so
$variable_name = 123;
function funct(&$var) {
$old = $var;
$var = $checksum = md5(time()); // give it unique value
foreach ($GLOBALS as $name => $value) {
if ($value == $var) {
echo $name; // will echo variable_name
$var = $old; // reassign old value
break;
}
}
}
so it is entirely possible :)
Based on PTBNL's (most definately correct) answer i came up with a more readable (at least i think so) approach:
/**
* returns the name of the variable posted as the first parameter.
* If not called from global scope, pass in get_defined_vars() as the second parameter
*
* behind the scenes:
*
* this function only works because we are passing the first argument by reference.
* 1. we store the old value in a known variable
* 2. we overwrite the argument with a known randomized hash value
* 3. we loop through the scope's symbol table until we find the known value
* 4. we restore the arguments original value and
* 5. we return the name of the symbol we found in the table
*/
function variable_name( & $var, array $scope = null )
{
if ( $scope == null )
{
$scope = $GLOBALS;
}
$__variable_name_original_value = $var;
$__variable_name_temporary_value = md5( number_format( microtime( true ), 10, '', '' ).rand() );
$var = $__variable_name_temporary_value;
foreach( $scope as $variable => $value )
{
if ( $value == $__variable_name_temporary_value && $variable != '__variable_name_original_value' )
{
$var = $__variable_name_original_value;
return $variable;
}
}
return null;
}
// prove that it works:
$test = 1;
$hello = 1;
$world = 2;
$foo = 100;
$bar = 10;
$awesome = 1;
function test_from_local_scope()
{
$local_test = 1;
$local_hello = 1;
$local_world = 2;
$local_foo = 100;
$local_bar = 10;
$local_awesome = 1;
return variable_name( $local_awesome, get_defined_vars() );
}
printf( "%s\n", variable_name( $awesome, get_defined_vars() ) ); // will echo 'awesome'
printf( "%s\n", test_from_local_scope() ); // will also echo awesome;
Sander has the right answer, but here is the exact thing I was looking for:
$contact_name = 'foo';
function do_something($args = array(), $another_arg) {
foreach ($args as $name => $value) {
echo $name;
echo '<br>'.$another_arg;
}
}
do_something(compact(contact_name),'bar');
class Someone{
protected $name='';
public function __construct($name){
$this->name=$name;
}
public function doSomthing($arg){
echo "My name is: {$this->name} and I do {$arg}";
}
}
//in main
$Me=new Someone('Itay Moav');
$Me->doSomething('test');