
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Class properties
Introduction
Data members declared inside class are called properties. Property is sometimes referred to as attribute or field. In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP. Value of property can be different for each instance of class. That's why it is sometimes referred as instance variable.
Inside any instance method, property can be accessed by calling object's context available as a pesudo-variable $this. If a property is declared as public, it is available to object with the help of -> operator. If a property is defined with static keyword, its value is shared among all objects of the class and is accessed using scope resolution operator (::) and name of class.
property declaration and access
This example shows how a property is defined and accessed
Example
<?php class myclass{ private $fname="Kiran"; public $mname="Pratap"; static $lname="Singh"; function dispdata(){ echo "$this->fname
"; echo "$this->mname
"; echo myclass::$lname; } } $obj=new myclass(); $obj->dispdata(); ?>
Output
The output of above code is as follows −
Kiran Pratap Singh
Outside class, instance properties declared as public are available to object, but private properties are not accessible. In previous versions of PHP, var keyword was available for property declaration. Though it has now been deprecated, it is still available for backward compatibility and is treated as public declaration of property.
PHP 7.4 introduces type declaration of property variables
Example
<?php class myclass{ private string $name; private int $age; function setdata(string $x, int $y){ $this->name=$x; $this->age=$y; } } $obj=new myclass("Kiran",20); ?>
- Related Articles
- PHP Inherit the properties of non-class object?
- Properties of the Thread Class
- PHP Static Properties and Methods
- PHP Class Abstraction
- PHP Class Constants
- PHP Closure class
- PHP Generator class
- PHP WeakReference class
- Explain abstract class in PHP.
- What are the properties of array class in C#?
- PHP Basics of Class and Object
- Can we define constant in class constructor PHP?
- Instance child class in abstract static method PHP?
- Explain final class and final method in PHP.
- How are the methods and properties of Array class in C# useful?
