Properties

Class member variables are called properties. They may be referred to using other terms such as fields, but for the purposes of this reference properties will be used. They are defined by using one of the keywords public, protected, or private, optionally, as of PHP 7.4, followed by a type declaration, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value.

See Visibility for more information on the meanings of public, protected, and private.

Note:

An alternative and not recommended way of declaring class properties, as it is to maintain backward compatibility with PHP 4, is by using the var keyword. It will treat the property identically as it would have been declared as public.

Within class methods non-static properties may be accessed by using -> (Object Operator): $this->property (where property is the name of the property). Static properties are accessed by using the :: (Double Colon): self::$property. See Static Keyword for more information on the difference between static and non-static properties.

The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is the value of the calling object.

Example #1 Property declarations

<?php
class SimpleClass
{
   public 
$var1 'hello ' 'world';
   public 
$var2 = <<<EOD
hello world
EOD;
   public 
$var3 1+2;
   
// invalid property declarations:
   
public $var4 self::myStaticMethod();
   public 
$var5 $myVar;

   
// valid property declarations:
   
public $var6 myConstant;
   public 
$var7 = [truefalse];

   public 
$var8 = <<<'EOD'
hello world
EOD;
}
?>

Note:

There are various functions to handle classes and objects. See the Class/Object Functions reference.

Type declarations

As of PHP 7.4.0, property definitions can include a Type declarations, with the exception of callable.

Example #2 Example of typed properties

<?php

class User
{
    public 
int $id;
    public ?
string $name;

    public function 
__construct(int $id, ?string $name)
    {
        
$this->id $id;
        
$this->name $name;
    }
}

$user = new User(1234null);

var_dump($user->id);
var_dump($user->name);

?>

The above example will output:

int(1234)
NULL

Typed properties must be initialized before accessing, otherwise an Error is thrown.

Example #3 Accessing properties

<?php

class Shape
{
    public 
int $numberOfSides;
    public 
string $name;

    public function 
setNumberOfSides(int $numberOfSides): void
    
{
        
$this->numberOfSides $numberOfSides;
    }

    public function 
setName(string $name): void
    
{
        
$this->name $name;
    }

    public function 
getNumberOfSides(): int
    
{
        return 
$this->numberOfSides;
    }

    public function 
getName(): string
    
{
        return 
$this->name;
    }
}

$triangle = new Shape();
$triangle->setName("triangle");
$triangle->setNumberofSides(3);
var_dump($triangle->getName());
var_dump($triangle->getNumberOfSides());

$circle = new Shape();
$circle->setName("circle");
var_dump($circle->getName());
var_dump($circle->getNumberOfSides());
?>

The above example will output:

string(8) "triangle"
int(3)
string(6) "circle"

Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization