What does the variable $this mean in PHP?
It's a reference to the current object, it's most commonly used in object oriented code.
- Reference: http://www.php.net/manual/en/language.oop5.basic.php
- Primer: http://www.phpro.org/tutorials/Object-Oriented-Programming-with-PHP.html
Example:
<?php
class Person {
public $name;
function __construct( $name ) {
$this->name = $name;
}
};
$jack = new Person('Jack');
echo $jack->name;
This stores the 'Jack' string as a property of the object created.
PHP, $this-{$var} -- what does that mean?
This is a variable variable, such that you will end up with $this->{value-of-$val}
.
See: http://php.net/manual/en/language.variables.variable.php
So for example:
$this->a = "hello";
$this->b = "hi";
$this->val = "howdy";
$val = "a";
echo $this->{$val}; // outputs "hello"
$val = "b";
echo $this->{$val}; // outputs "hi"
echo $this->val; // outputs "howdy"
echo $this->{"val"}; // also outputs "howdy"
Working example: http://3v4l.org/QNds9
This of course is working within a class context. You can use variable variables in a local context just as easily like this:
$a = "hello";
$b = "hi";
$val = "a";
echo $$val; // outputs "hello"
$val = "b";
echo $$val; // outputs "hi"
Working example: http://3v4l.org/n16sk
What does 'this followed by square brackets' mean in PHP?
The fact that your variable is called $this
means that it can't be an ordinary array - that variable name is reserved for the current instance of a class.
Using square brackets to access an object is a sign that the class implements that ArrayAccess
interface - that is, it can be accessed using the operators normally reserved for basic arrays.
Retrieving a value from a class that implements ArrayAccess
using square bracket notation invokes the class's offsetGet
method, with the provided key as the $offset
argument. The most common use is to allow access to a class's member variables, but the class itself can choose to perform any action in this method.
In your case, I think you're looking at the Laravel application class, which will result in the router item being returned from the dependency injection container.
What is the meaning of $this
$this
is a reference to the current object.
It can be used in class methods only.
From the manual:
The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).
a simple real world example:
class Classname
{
private $message = "The big brown fox... jumped....";
function setMessage($givenMessage) {
$this->message = $givenMessage;
}
function getMessage() {
return $this->message; // Will output whatever value
// the object's message variable was set to
}
}
$my_object = new Classname(); // this is a valid object
echo $my_object->getMessage(); // Will output "The big brown fox... jumped...."
$my_object->setMessage("Hello World!");
echo $my_object->getMessage(); // Will output "Hello world"
$this
is not available when you call a method in a static context:
Classname::showMessage(); // Will throw an error:
// `$this` used while not in object context
Reference — What does this symbol mean in PHP?
Incrementing / Decrementing Operators
++
increment operator
--
decrement operator
Example Name Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
These can go before or after the variable.
If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.
For example:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
Live example
In the case above ++$i
is used, since it is faster. $i++
would have the same results.
Pre-increment is a little bit faster because it really increments the variable and after that 'returns' the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second's.
However, you must use $apples--
, since first, you want to display the current number of apples, and then you want to subtract one from it.
You can also increment letters in PHP:
$i = "a";
while ($i < "c") {
echo $i++;
}
Once z
is reached aa
is next, and so on.
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
Stack Overflow Posts:
- Understanding Incrementing
What does $this-{$key} mean in PHP?
Reads the idkey
property of the $this
object:
$this->idKey;
Reads the variable property name of the $this
object (example
in this case) so $this->example
:
$idKey = 'example';
$this->$idKey;
Same as above ($this->example
), but with less ambiguity (similar to adding parentheses to control operand order, and useful in some cases):
$idKey = 'example';
$this->{$idKey};
A case where this may add clarity or control the order:
$this->{$idKey['key']};
$this->{$idKey}['key'];
Related Topics
Call to a Member Function Bind_Param() on a Non-Object
Laravel Requires the Mcrypt PHP Extension
PHP - Check If Two Arrays Are Equal
How to Mathematically Evaluate a String Like "2-1" to Produce "1"
Delimiter Must Not Be Alphanumeric or Backslash and Preg_Match
How to Get an Array of Specific "Key" in Multidimensional Array Without Looping
Using Jquery $.Ajax to Call a PHP Function
Permutations - All Possible Sets of Numbers
How to Get the Query Builder to Output Its Raw SQL Query as a String
Angularjs Http Post to PHP and Undefined
Detecting Request Type in PHP (Get, Post, Put or Delete)
Https and Ssl3_Get_Server_Certificate:Certificate Verify Failed, Ca Is Ok
Preg_Match(): Compilation Failed: Invalid Range in Character Class At Offset
Woocommerce: Auto Complete Paid Orders
Suppress Error With @ Operator in PHP