var
In PHP, the "var" keyword is used to declare class properties. It was commonly used in earlier versions of PHP but has since been replaced by the "public",
The PHP var Keyword
var is a legacy keyword for declaring a class property in PHP. It dates back to PHP 4, when classes had no access modifiers and every property was effectively public. Modern PHP keeps var only for backward compatibility — since PHP 5 it is an exact synonym for public.
This page covers what var does, how it compares with the public / private / protected modifiers that replaced it, and why you should not reach for it in new code.
var is the same as public
The key fact to remember: a property declared with var behaves identically to one declared with public. Both are readable and writable from anywhere — inside the class, in subclasses, and from outside.
<?php
class User {
var $name = "Guest"; // legacy syntax
}
class Account {
public $name = "Guest"; // modern equivalent — exactly the same behavior
}
$a = new User();
$b = new Account();
echo $a->name . "\n"; // Guest
echo $b->name . "\n"; // Guest
$a->name = "Alice"; // both properties are publicly writable
$b->name = "Bob";
echo $a->name . "\n"; // Alice
echo $b->name . "\n"; // BobBecause they are equivalent, mixing var and public in the same class is legal but confusing — pick public and stay consistent.
Syntax
A var property may be declared with or without a default value, and you can list several on one line:
<?php
class Config {
var $host; // no default → null
var $port = 8080; // with a default value
var $debug = false, $cache = true; // multiple at once
}
$c = new Config();
var_dump($c->host); // NULL
echo $c->port . "\n"; // 8080
var_dump($c->debug); // bool(false)A default value must be a constant expression (a literal, a constant, or an array of them) — it cannot be the result of a function call or another object.
Why var was replaced
PHP 5 introduced real visibility with three access modifiers, and var simply could not express the difference between them:
| Modifier | Accessible from |
|---|---|
public | anywhere (same as var) |
protected | the class itself and its subclasses |
private | only the declaring class |
<?php
class BankAccount {
public $owner; // readable everywhere
protected $type; // class + subclasses
private $balance = 0; // this class only
public function deposit(int $amount): void {
$this->balance += $amount; // allowed: same class
}
public function getBalance(): int {
return $this->balance;
}
}
$acc = new BankAccount();
$acc->owner = "Alice"; // OK — public
$acc->deposit(100);
echo $acc->getBalance(); // 100
// echo $acc->balance; // Fatal error: Cannot access private propertyEncapsulating $balance as private forces all changes to go through deposit(), so the class controls its own state. That guarantee is exactly what var cannot give you, which is why explicit modifiers became the standard.
Gotchas
- It is not
varfrom JavaScript. PHP'svaronly declares a class property. Local variables inside functions are written$x = 1;with no keyword. - Don't combine it with a modifier.
var public $x;is a syntax error —varis the modifier. - Typed properties don't use it. Since PHP 7.4 you write
public int $port;. There is novar int $port;.
Best practices
Avoid var in new code. Always declare visibility explicitly so the reader knows the intended access level at a glance:
- Default to the most restrictive visibility that still works — usually
private, exposing data through methods. - Use
protectedwhen subclasses legitimately need access. - Reserve
publicfor the parts that are truly part of the object's external interface.
Related topics
- PHP Access Modifiers —
public,private, andprotectedin depth. - PHP
publicand PHPprivatekeywords. - PHP Classes and Objects and the
classkeyword. - PHP Constructor — initializing properties when an object is created.
Conclusion
var is a holdover from PHP 4 that now means nothing more than public. It still works, but it hides your intent. In modern PHP, declare every property with public, protected, or private so your code's access rules are explicit and self-documenting.