This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.

Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.

The type can also be made nullable by adding ? before it. This is identical to a type union with null.

foo(?string $var) { }
// is the same as
foo(string|null $var) { }

You can also use composite types such as string|int as of PHP 8.0.

An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.

Answer from JW. on Stack Overflow
🌐
PHP
php.net › manual › en › language.variables.variable.php
PHP: Variable variables - Manual
This information becomes relevant when writing a parser, tokenizer or something else that operates on PHP syntax. <?php $foo = 'bar'; $ /* I am complete legal and will compile without notices or error as a variable variable. */ $foo = 'magic'; echo $bar; // Outputs magic.
🌐
W3Schools
w3schools.com › pHP › php_variables.asp
PHP Variables
A variable can have a short name (like $x and $y) or a more descriptive name ($age, $carname, $total_volume). ... Remember that PHP variable names are case-sensitive!
Discussions

Declaring Variable Types in PHP? - Stack Overflow
I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP: function blah(Bur $bur) {} A couple of questions: Does this actually impose any limits on what More on stackoverflow.com
🌐 stackoverflow.com
What is the proper way to declare variables in php? - Stack Overflow
You need to declare variables before echoing them out. An example is here: More on stackoverflow.com
🌐 stackoverflow.com
What is the use case for variable variables?
To be honest, they're not very useful in real world situations. Usually variable variables are a code smell. If you see them in a real world situation, it's -usually- because someone took a bad shortcut. I suppose in certain situations they might be useful for unit testing... but that's a stretch. More on reddit.com
🌐 r/PHPhelp
13
1
July 20, 2023
class - What does the variable $this mean in PHP? - Stack Overflow
I see the variable $this in PHP all the time and I have no idea what it's used for. I've never personally used it. Can someone tell me how the variable $this works in PHP? More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-variables
PHP Variables - GeeksforGeeks
April 11, 2025 - A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects.
Top answer
1 of 5
91

This type-hinting only works for validating function arguments, return values, properties, and class constants; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when blah is called, but $bur could be reassigned to a non-Bur value inside the function.

Type-hinting works for any data type except for resources. Initially, it was available only with class names, but later, support for array (as of PHP 5.1), callable (as of PHP 5.4), string, int, float, bool (as of PHP 7.0) was added. Since then, PHP has added support for even more types, which you can see listed in the PHP manual.

The type can also be made nullable by adding ? before it. This is identical to a type union with null.

foo(?string $var) { }
// is the same as
foo(string|null $var) { }

You can also use composite types such as string|int as of PHP 8.0.

An important distinction is that, unless your PHP file is in strict mode, the arguments that you pass to a function call will be type-juggled to the appropriate scalar type if possible. To avoid this, your PHP file must use strict typing mode.

2 of 5
45
  1. Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
  2. Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.

/**
 * @var string
 */
public $variable = "Blah";

As of PHP 7 (which is several years old at this point), primitive types can also be declared for function arguments. Nullability can also be indicated with a ? in front of the type from 7.1 onward. You can declare return types now, too. So this is valid PHP these days:

public function hasFoo(?int $numFoos) :bool {

PhpStorm (my current preferred IDE) is happy to use all of these types for code completion, so I don't need as many phpDoc comments for typing as I used to.

🌐
Codecademy
codecademy.com › learn › learn-php › modules › learn-php-variables › cheatsheet
Learn PHP: Learn PHP Variables Cheatsheet | Codecademy
Variable names can contain numbers, letters, and underscores (_). A sigil ($) must always precede a variable name. They cannot start with a number and they cannot have spaces or any special characters. The convention in PHP is to use snake case for variable naming; this means that lowercase words are delimited with an underscore character (_).
🌐
Tutorialspoint
tutorialspoint.com › php › php_variable_types.htm
PHP - Variables
Variables in PHP are used to store data that can be accessed and modified across the program. A variable can store a wide range of values, like numbers, text, arrays and even objects.
Find elsewhere
🌐
Codecademy
codecademy.com › learn › learn-php-introduction › modules › php-strings-numbers-variables › cheatsheet
Learn PHP: Introduction: PHP Strings, Numbers, and Variables Cheatsheet | Codecademy
Variable names can contain numbers, letters, and underscores (_). A sigil ($) must always precede a variable name. They cannot start with a number and they cannot have spaces or any special characters. The convention in PHP is to use snake case for variable naming; this means that lowercase words are delimited with an underscore character (_).
🌐
W3Schools
w3schools.com › php › php_variables_scope.asp
PHP Variable Scope
To do this, use the global keyword before the variables (inside the function): $x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; // outputs 15 Try it Yourself » · PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable.
🌐
YouTube
youtube.com › dani krossing
4: How to Create PHP Variables | PHP Tutorial | Learn PHP Programming | PHP for Beginners - YouTube
How to Create PHP Variables | PHP Tutorial | Learn PHP Programming | PHP for Beginners. In this PHP tutorial you will learn about variables which are used to...
Published   October 22, 2015
Views   235K
🌐
In Easy Steps
ineasysteps.com › home › old › creating variables in php 7
Creating Variables in PHP 7 - In Easy Steps
May 26, 2022 - A “variable” is a named container in a PHP script in which a data value can be stored. The stored value can be referenced using the variable’s name and changed (varied) as the script proceeds.
🌐
Shodor
shodor.org › ~kevink › phpTutorial › destinye_PHPvariables.php
PHP Variables
PHP variables are characters that stores value or information such as text or integers in your code. It is important to know that variables in PHP are usually represented by a dollar sign($) followed by the name of the variable. Those variables can be used to hold values or expressions and ...
🌐
SitePoint
sitepoint.com › blog › php › php variables
phpmaster | PHP Variables
November 11, 2024 - There are a few standard variables that PHP creates automatically, but most of the time variables are created (or declared) by you, the programmer. By creating two variables called $name and $color, you could create generic code which can handle any input values.
Top answer
1 of 11
148

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.

2 of 11
47

The best way to learn about the $this variable in PHP is to try it against the interpreter in various contexts:

print isset($this);              //true,   $this exists
print gettype($this);            //Object, $this is an object 
print is_array($this);           //false,  $this isn't an array
print get_object_vars($this);    //true,   $this's variables are an array
print is_object($this);          //true,   $this is still an object
print get_class($this);          //YourProject\YourFile\YourClass
print get_parent_class($this);   //YourBundle\YourStuff\YourParentClass
print gettype($this->container); //object
print_r($this);                  //delicious data dump of $this
print $this->yourvariable        //access $this variable with ->

So the $this pseudo-variable has the Current Object's method's and properties. Such a thing is useful because it lets you access all member variables and member methods inside the class. For example:

Class Dog{
    public $my_member_variable;                             //member variable

    function normal_method_inside_Dog() {                   //member method

        //Assign data to member variable from inside the member method
        $this->my_member_variable = "whatever";

        //Get data from member variable from inside the member method.
        print $this->my_member_variable;
    }
}

$this is reference to a PHP Object that was created by the interpreter for you, that contains an array of variables.

If you call $this inside a normal method in a normal class, $this returns the Object (the class) to which that method belongs.

It's possible for $this to be undefined if the context has no parent Object.

php.net has a big page talking about PHP object oriented programming and how $this behaves depending on context. https://www.php.net/manual/en/language.oop5.basic.php

🌐
Quora
quora.com › What-is-a-PHP-variable-and-how-is-it-declared-1
What is a PHP variable, and how is it declared? - Quora
Answer: Variables in a program are used to store some values or data that can be used later in a program. The variables are also like containers that store character values, numeric values, memory addresses, and strings.
🌐
W3Schools
w3schools.com › php › php_datatypes.asp
PHP Data Types
Casting allows you to change data type on variables: $x = 5; $x = (string) $x; var_dump($x); Try it Yourself » · You will learn more about casting in the PHP Casting Chapter.