I do not know this symbol PHP [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
many times I've seen this symbol ->
What is it? seems a logical operator or something
Complete example:
get_categories->category_count

It's called the object operator.
For example:
$myObject = new stdClass();
$myObject->Hello = "world";
echo $myObject->Hello; //Returns world
You can also use this operator to call properties or methods from classes. For example:
Class MyClass {
public function hello(){
return "world";
}
}
echo (new MyClass())->hello(); //Returns world

It is called as PHP object operator (T_OBJECT_OPERATOR).
Example:
$car = new Car();
$car->color = 'Red';
echo $car->color; // returns the color of the car.

Related

Problems with syntax in PHP [duplicate]

This question already has answers here:
How to concatenate PHP variable name?
(4 answers)
Closed 6 years ago.
I have an object and can access one of its properties by
$book = new Book(); print $book->price;
I can also call
$prop = "price"; print $book->$prop.
But I cannot figure out how to call in a mixed way, it gives error:
$book->id_$prod
as a shortened of $book->id_products, being $prod = "products".
$book = new Book();
$id_prod = "id_$prod";
print $book->$id_prod;
Mix the 2 first options.
$prop = 'id_'.$prod;
$book->$prop;

Is it a bug in PHP with assignment by reference? [duplicate]

This question already has answers here:
PHP param by ref => assign to ref = NULL
(1 answer)
PHP's assignment by reference is not working as expected
(7 answers)
Closed 8 years ago.
Here is the simplified version of code that might be revealing a PHP bug
class AClass
{
public static $prop = "Hi";
}
function assignRef (&$ref)
{
$ref = &AClass::$prop;
echo "inside assignRef: $ref\n";
}
$ref = "Hello";
assignRef($ref);
echo "outside: $ref\n";
This prints out
inside assignRef: Hi
outside: Hello
Shouldn't $ref had been assigned by reference to $prop static variable of the AClass class and become "Hi" not just inside assignRef function but also outside of it?
The class in your example is irrelevant, simplified version that produces the same output:
function assignRef (&$ref)
{
$prop = 'Hi';
$ref = &$prop;
echo "inside assignRef: $ref\n";
}
$ref = "Hello";
assignRef($ref);
echo "outside: $ref\n";
What is happening is that when assigning by reference inside the function ($ref = &$prop;) you are just changing what one variable is pointing to, not changing the value of what it was originally pointing to nor changing any other references to that original value.
You effectively have two variables called $ref in this example - one inside the function, and one outside the function. You are changing what the variable inside the function points to, leaving the other variable pointing to the original (unchanged) value.
Consider the following code:
$a = 'a';
$b = 'b';
$c = 'c';
$a = &$b;
$b = &$c;
echo "$a / $b / $c";
This results in output of b / c / c, rather than what you might expect c / c / c. This happens for the same reason - assignment by reference does not affect the value originally referenced nor change any other references, meaning any other variables pointing to the original value are unchanged.
If you want to change the value, rather than creating a new reference to another value, you must use normal assignment (=). Alternatively, you could change all references.

meaning of &$variable and &function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
what is the meaning of &$variable
and meaning of functions like
function &SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 )
{
$rs =& $this->do_query( $sql, $offset, $nrows, $inputarr);
return $rs;
}
Passing an argument like so: myFunc(&$var); means that the variable is passed by reference (and not by value). So any modifications made to the variable in the function modify the variable where the call is made.
Putting & before the function name means "return by reference". This is a bit very counter-intuitive. I would avoid using it if possible. What does it mean to start a PHP function with an ampersand?
Be careful not to confuse it with the &= or & operator, which is completely different.
Quick test for passing by reference:
<?php
class myClass {
public $var;
}
function incrementVar($a) {
$a++;
}
function incrementVarRef(&$a) { // not deprecated
$a++;
}
function incrementObj($obj) {
$obj->var++;
}
$c = new myClass();
$c->var = 1;
$a = 1; incrementVar($a); echo "test1 $a\n";
$a = 1; incrementVar(&$a); echo "test2 $a\n"; // deprecated
$a = 1; incrementVarRef($a); echo "test3 $a\n";
incrementObj($c); echo "test4 $c->var\n";// notice that objects are
// always passed by reference
Output:
Deprecated: Call-time pass-by-reference has been deprecated; If you would like
to pass it by reference, modify the declaration of incrementVar(). [...]
test1 1
test2 2
test3 2
test4 2
The ampersand - "&" - is used to designate the address of a variable, instead of it's value. We call this "pass by reference".
So, "&$variable" is the reference to the variable, not it's value. And "function &func(..." tells the function to return the reference of the return variable, instead of a copy of the variable.
See also:
difference between function and &function
http://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference
http://www.php.net/manual/en/language.references.pass.php
http://www.adp-gmbh.ch/php/pass_by_reference.html

How can I call a class with variables from array php? [duplicate]

This question already has answers here:
Is there a call_user_func() equivalent to create a new class instance?
(2 answers)
Closed 8 years ago.
I would like something like this but dynamically from an array like this:
$array = array("first","second","third");
So class would be called like this:
$class = new class("first","second","third");
You can use reflection for it:
$refClass = new ReflectionClass('SomeClass');
$instance = $refClass->newInstanceArgs($args);

How to use the -> in PHP [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does this php construct mean: $html->redirect(“URL”)?
Hi, I've been looking for what this operator "->" does, but I can't seem to find a reference to it, only people using it in come and I can't make out what its doing, an explanation would be appreciated.
The -> is used with object methods/properties, example:
class foo{
function bar(){
echo 'Hello World';
}
}
$obj = new foo;
$obj->bar(); // Hello World
More Info:
http://de.php.net/manual/en/language.oop5.basic.php
http://php.net/manual/en/tokens.php
It's for classes.
See here:
http://de.php.net/manual/en/language.oop5.basic.php
-> operator access properties and methods of an object.
Probably you should read PHP OOP introduction: http://php.net/manual/en/language.oop5.php
Expanding on sarfraz's answer to demonstrate how to access properties:
class foo{
public $value = "test";
function bar(){
//// code
}
}
$obj = new foo;
$obj->bar();
echo $obj->value; //displays "test"

Categories