Welcome to this comprehensive guide on PHP Properties and Methods! This tutorial is designed for both beginners and intermediates, providing a deep dive into the world of PHP object-oriented programming. Let's get started!
In PHP, properties are variables that are defined within a class and belong to an object of that class. They hold the state of the object, allowing you to store and access data.
class ExampleClass {
public $property; // This is a public property
}
$obj = new ExampleClass();
$obj->property = 'This is a property'; // Setting a property value
echo $obj->property; // Accessing a property valueπ Note:
public, protected, private, and static. We'll delve into each of these later in this tutorial.Methods are functions that are defined within a class and are used to perform actions on objects. They can manipulate properties, perform calculations, and more.
class ExampleClass {
public $property;
public function exampleMethod() { // This is a method
echo $this->property; // Accessing a property within a method
}
}
$obj = new ExampleClass();
$obj->property = 'This is a property';
$obj->exampleMethod(); // Calling the methodYou can access properties and methods using the arrow operator (->) and the object instance.
class ExampleClass {
public $property;
public function exampleMethod() {
echo $this->property;
}
}
$obj = new ExampleClass();
$obj->property = 'This is a property';
$obj->exampleMethod(); // Output: This is a propertyIn PHP, variables declared within a class are called class variables, and they retain their values even after the object is destroyed. On the other hand, variables declared within a method are called instance variables, and their scope is limited to the method in which they are declared.
class ExampleClass {
public static $classVar = 'This is a class variable'; // Class variable
public function exampleMethod() {
$instanceVar = 'This is an instance variable'; // Instance variable
echo $this->classVar; // Output: This is a class variable
echo $instanceVar; // Output: This is an instance variable
}
}
$obj1 = new ExampleClass();
$obj2 = new ExampleClass();
echo $obj1->exampleMethod(); // Output: This is a class variable This is an instance variable
echo $obj2->exampleMethod(); // Output: This is a class variable This is an instance variableAs mentioned earlier, PHP supports four types of properties:
public keyword can be accessed from anywhere.class ExampleClass {
public $publicProperty;
}
$obj = new ExampleClass();
$obj->publicProperty = 'This is a public property';
echo $obj->publicProperty; // Output: This is a public propertyprotected keyword can be accessed within the class and its child classes.class ExampleClass {
protected $protectedProperty;
}
class ChildClass extends ExampleClass {
function displayProtected() {
echo $this->protectedProperty;
}
}
$obj = new ChildClass();
$obj->protectedProperty = 'This is a protected property';
$obj->displayProtected(); // Output: This is a protected propertyprivate keyword can only be accessed within the class in which they are defined.class ExampleClass {
private $privateProperty;
function displayPrivate() {
echo $this->privateProperty;
}
}
$obj = new ExampleClass();
$obj->privateProperty = 'This is a private property'; // Error: private properties are not accessible from outside the class
$obj->displayPrivate(); // Output: This is a private propertystatic keyword are class-level properties and can be accessed without creating an instance of the class.class ExampleClass {
static public $staticProperty = 'This is a static property';
}
echo ExampleClass::$staticProperty; // Output: This is a static propertyWhat are PHP properties used for?
What is the difference between class variables and instance variables in PHP?