This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 7 years ago.
I am studying how to connect database while learning PHP. Just a quick question. Does anyone can tell me what does "->" sign do in PHP? I cannot understand the functionality of this sign so that I have no idea how to edit the code. Thank whoever answer this.
-> Sign used in objects, to access it's property and methods.
class example{
public $prop1 = 'Hello World';
public function sayHello(){
echo $this->prop1;
}
}
$example = new example();
$example->sayHello();
Ref: Classes and Objects in PHP
-> Is Used to refer the Classes And Objects for more information check here.
You haven't posted any code, so I'm not 100% sure where you saw this, but I'm almost certain you are referring to something like this:
$foo = new Foo();
echo $foo->bar;
In this example, -> is used to access a property of an object, $foo. It can also be used to access a method, as in $foo->baz();.
For real quick and dirty one-liner anonymous objects, just cast an associative array:
<?php
$obj = (object) array('foo' => 'bar', 'property' => 'value');
echo $obj->foo; // prints 'bar'
echo $obj->property; // prints 'value'
?>
... no need to create a new class or function to accomplish it.
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 5 years ago.
class ClassOne
{
private $foo = null;
public function __construct()
{
$this->foo = new ClassTwo();
}
public function doStuff()
{
$bar = &$this->foo->someVariable;
}
}
I know that $this refers to the current object, and & is a reference symbol for changing the original variable.
What is &$this referring to?
Thanks to #k0pernikus and #trincot for clarifying that & refers to the entire $this->foo->someVariable
So the original author wanted to change someVariable in $this->foo->someVariable. Since it was verbose, he assigned the reference to $bar.
However, this was done in PHP4. There must be a better way to write this in PHP5.
This question already has answers here:
How and why use curly braces: return $this->{$this->action}();
(2 answers)
Closed 6 years ago.
Feel free to re-title this Question because I do not know the proper name for doing this. More to the point, I have seen people using {'property'} when accessing a property inside an object so I set-up an example to try understand however, the property is accessible when I use it and when I don't?
class Example {
public $name;
}
$e = new Example();
$e->{'name'} = 'Kdot';
echo $e->name; // output: Kdot
I have tried changing the scopes and accessing it through a class method but it works both ways, again.
Can someone help me understand what the meaning of using the {} delimiters are? Because from my knowledge, if you stored the parameter inside another variable, this would also work:
$property = 'name';
echo $e->$property; // output: Kdot
They're mostly both just different ways to do the same thing, but with slightly different capabilities.
One difference is that if you want to concatenate inline and use that for a property name, you need the braces:
// example:
$property = 'foo';
echo $e->{$property . 'Suffix'}; // good, uses $e->fooSuffix
echo $e->$property . 'Suffix' // bad, uses $e->foo and adds "Suffix" literal
As well as that, directly access property names need to conform to certain rules. For example if you have a dash in your property name you need to use braces to access it.
This question already has answers here:
Stop using `global` in PHP
(6 answers)
Closed 7 years ago.
The $GLOBALS array of PHP provides access to all global variables, like
<?php
$foo = 'hello';
function myFunc() {
echo $GLOBALS['foo']; // prints "hello"
}
?>
Now I have to work on some code from other people that directly adds elements to the array to have it available globally (without having an according variable), like so:
<?php
function doSomething() {
// $newData is NOT existing anywhere!
$GLOBALS['newData'] = 'Hello World!';
// now $GLOBALS['newData'] is available anywhere else w/o actual variable
}
?>
Until now I never saw this specific usage of the $GLOBALS array and was wondering if it is considered "safe" or "good"? The PHP manual makes no statement about writing to this array, only about reading from it.
Opinion perhaps, but I would think it's better instead to use define instead of the $GLOBALS array if you're looking to define a constant for use in other parts of your application.
PHP:define - Manual
<?php
function doSomething() {
$_POST['__newData'] = 'Hello World!';
}
doSomething();
echo $_POST['__newData'];
?>
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What is this called in php: -> [duplicate]
(4 answers)
Closed 9 years ago.
I am trying to learn how a specific project works and while I can find most functions online, I found one that stumps me.
I see "->" appear every so often in the code, but have no idea what it does.
What does "->" mean in PHP or Joomla?
It's the object operator in PHP. It is used to access child properties and methods of classes. Its Javascript and Java equivalent is the . operator. It would be used in PHP like this
class foo{
public $bar="qux";
public function display(){
echo $this->bar;
}
}
$myFoo=new foo();
$myFoo->display(); //displays "qux"
Its like the . operator in C++ and Java. Refers to members within a class. C++ also uses the -> to access members when the variable preceding the -> is a pointer to a class rather than an instance of the class.
-> is the way used to call a method.
In C, C++, C#, Java you use . (dot) notation to call a method:
operation.sum(a, b);
In php you do it using ->
operation->sum(a,b)
This question already has answers here:
What does $$ (dollar dollar or double dollar) mean in PHP?
(7 answers)
Closed 8 years ago.
what does two back to back $ behind a variable means. Like this
$$id
where can I find more information on that
Thanks
In PHP, $$ means you are about to inflict years of pain and suffering on at least one maintenance programmer. Note that you might wind up being that maintenance programmer.
It is a variable variable. Imagine this:
$quux = 'bar';
$foo[$quux] = "baz";
echo $foo['bar']; //prints baz
if there was no such thing as arrays, you might try something like this:
$quux = 'bar';
$$quux = "baz";
echo $bar; //prints baz
luckily we do have arrays so please don't use variable variables unless you are doing something convoluted and magical* and have no other choice.
*: Please don't do convoluted magical things, either.
These are called variable variables.
$foo = 'bar';
$id = 'foo';
echo $id; // prints foo
echo $$id; // prints bar
in the PHP manual of course
http://www.php.net/manual/en/language.variables.variable.php
note that it's obsolete and senseless syntax and you should always use arrays instead.