Question
What does the var keyword mean in PHP? In particular, how is it used when declaring a class property, and are there differences in its meaning or use between PHP 4 and PHP 5?
Short Answer
By the end of this page, you will know that var is a legacy way to declare a class property in PHP, why it is equivalent to public in PHP 5 and later, and why modern code normally uses explicit visibility keywords instead.
Concept
var is used inside a PHP class to declare a property (also called a member variable).
class User
{
var $name = "Guest";
}
In PHP 4, var was the normal syntax for declaring class properties. PHP 4 did not have property visibility keywords such as public, protected, and private.
In PHP 5 and later, PHP added visibility. The var keyword was retained for backward compatibility and means the same as public when declaring a property:
class User
{
var $name = "Guest"; // Legacy syntax
public $email = ""; // Modern equivalent
}
Both properties are publicly accessible. Modern PHP code should usually use public, because it clearly communicates the intended visibility and fits with protected and private.
var is not the normal way to create local variables or global variables. PHP variables are created by assigning to names that begin with $.
Mental Model
Think of a class as a blueprint for a house, and properties as items stored in each house.
var $nameis an old label saying: “This house has a name field that anyone can access.”public $nameis the newer, clearer label with the same access level.protected $namemeans only the house and its specialized versions can access it.private $namemeans only this exact house design can access it.
In PHP 4, the old var label was the available choice. In PHP 5+, use the more descriptive labels.
Syntax and Examples
Declare properties inside a class body:
class Product
{
var $name = "Unknown"; // Legacy; equivalent to public
public $price = 0.0; // Preferred modern style
}
$product = new Product();
echo $product->name; // Unknown
$product->price = 19.99;
echo $product->price; // 19.99
-> accesses a property on an object.
In modern PHP, prefer explicit visibility:
class Account
{
public string $owner;
private float $balance = 0.0;
public function __construct(string $owner)
{
$this->owner = $owner;
}
}
Here, public allows outside code to read or write $owner, while private prevents code outside Account from directly changing $balance.
Step by Step Execution
Consider this code:
class Counter
{
var $count = 0;
public function increment(): void
{
$this->count++;
}
}
$counter = new Counter();
$counter->increment();
echo $counter->count;
Execution flow:
- PHP reads the
Counterclass definition.var $count = 0declares a public property with a default value of0. new Counter()creates an object. Its$countproperty starts at0.$counter->increment()calls the method on that object.- Inside the method,
$thisrefers to the currentCounterobject. $this->count++increases the property from0to1.echo $counter->countaccesses the public property and prints1.
Replacing var $count = 0; with public $count = 0; produces the same result in PHP 5 and later.
Real World Use Cases
Although new code generally avoids var, you may encounter it when:
- Maintaining a legacy PHP 4 or early PHP 5 application.
- Reading an old library, plugin, or framework.
- Migrating an older application to a current PHP version.
- Replacing unclear public data fields with better encapsulated class design.
For example, an old configuration object might contain:
class LegacyConfig
{
var $debug = false;
var $databaseHost = "localhost";
}
A modern version can make the public intent explicit:
class Config
{
public bool $debug = false;
public string $databaseHost = "localhost";
}
Real Codebase Usage
In modern PHP codebases, developers usually avoid var and choose a visibility level deliberately.
Public properties for simple data
class PageData
{
public string $title;
}
Private properties with methods to protect rules
class BankAccount
{
private float $balance = 0.0;
public function deposit(float $amount): void
{
if ($amount <= 0) {
throw new InvalidArgumentException("Deposit must be positive.");
}
$this->balance += $amount;
}
public function getBalance(): float
{
return $this->balance;
}
}
This pattern prevents invalid changes such as directly setting a balance to a negative number. Explicit visibility is important because it documents which parts of an object are intended for outside use.
When modernizing old code, a safe first step is often changing var to public. Later, decide whether each property should actually be protected or private.
Common Mistakes
Using var for a local variable
This is not how normal PHP variables are declared:
// Incorrect
function greet()
{
var $name = "Ada";
}
Use assignment instead:
function greet()
{
$name = "Ada";
echo $name;
}
Assuming var makes a variable dynamically typed
PHP variables are dynamically typed, but that is unrelated to the var keyword. In PHP, var is primarily legacy class-property syntax.
Treating var as different from public in PHP 5+
For a property declaration, these have the same public visibility:
var $status = "new";
public $status = "new";
Prefer public in new code for readability.
Combining var with another visibility keyword
Do not write both:
// Incorrect
public var $name;
Choose one visibility declaration. In modern code, choose public, protected, or private.
Making every property public
A property being easy to access does not mean it should be public. Use private when the class must control how a value changes.
Comparisons
| Declaration | Meaning in PHP 5+ | Accessible from outside the object? | Recommended for new code? |
|---|---|---|---|
var $name; | Legacy alias of public $name; | Yes | No |
public $name; | Public property | Yes | Yes, when public access is appropriate |
protected $name; | Property available to the class and subclasses | No | Yes, when inheritance needs access |
private $name; | Property available only inside its declaring class | No | Yes, for internal state |
PHP 4 versus PHP 5+:
| PHP version | Role of var |
|---|---|
| PHP 4 | Standard syntax for declaring a class property; visibility keywords were not available. |
| PHP 5+ | Kept for compatibility; acts as an alias for public on a class property. |
For typed properties in current PHP, use explicit modern syntax:
public string $name;
private int $quantity = 0;
Cheat Sheet
class Example
{
var $oldStyle = "value"; // Legacy; public property
public $newStyle = "value"; // Preferred equivalent
protected $childAccess = ""; // Class and subclasses
private $internal = ""; // Declaring class only
}
vardeclares a class property.- In PHP 5 and later,
varmeanspublicfor properties. - In PHP 4,
varwas the usual property declaration syntax. - Use
public,protected, orprivatein new PHP code. - Do not use
varto declare local variables. - Prefer explicit visibility, especially with typed properties:
public string $name;
private int $id;
FAQ
Is var deprecated in PHP?
var remains supported as a legacy alias for public when declaring class properties. However, new code should normally use public because it is clearer and consistent with other visibility keywords.
Does var mean the same as public in PHP?
For class property declarations in PHP 5 and later, yes. var $name; is equivalent to public $name;.
Can I use var outside a class in PHP?
No. Normal local and global PHP variables are created by assignment, such as $name = "Ada";.
What did var do in PHP 4?
It declared a class property. PHP 4 did not provide public, protected, or private property visibility keywords.
Should I replace var with public in old PHP code?
Usually, yes. Replacing var with public preserves the public-access behavior in PHP 5+ and makes the code easier to understand. Review whether a property should instead be private or protected before changing its visibility.
Can a var property be accessed with ->?
Yes. Since it is public in PHP 5+, outside code can access it through an object, for example $user->name.
Why use private instead of public or var?
private lets a class protect its internal state and enforce validation through methods. This reduces accidental or invalid changes from outside code.
Mini Project
Description
Modernize a small legacy-style class that uses var. The project demonstrates that var properties are publicly accessible, then uses explicit visibility and a method to protect an internal value.
Goal
Create a Task object whose title is public while its completion state can only be changed through methods.
Requirements
Define a Task class with a public string title property.
Initialize each task as incomplete.
Add a public method that marks the task as complete.
Add a public method that reports whether the task is complete.
Create a task, mark it complete, and print its status.
Keep learning
Related questions
Are PDO Prepared Statements Enough to Prevent SQL Injection in PHP?
Learn how PDO prepared statements prevent SQL injection in PHP, what they protect, and the mistakes that still leave MySQL apps vulnerable.
Can You Bind an Array to an IN Clause in PHP PDO?
Learn how PDO handles placeholders in IN() clauses, why arrays cannot be bound directly, and the safe PHP pattern to build dynamic queries.
Choosing the Right MySQL Collation for PHP and UTF-8
Learn how MySQL character sets and collations work with PHP, and how to choose a practical UTF-8 setup for web applications.