PHP Properties and Methods 🎯

beginner
15 min

PHP Properties and Methods 🎯

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!

Understanding Properties πŸ’‘

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.

php
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:

  • PHP supports four types of properties: public, protected, private, and static. We'll delve into each of these later in this tutorial.

Exploring Methods πŸ’‘

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.

php
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 method

Accessing Properties and Methods πŸ’‘

You can access properties and methods using the arrow operator (->) and the object instance.

php
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 property

Understanding Class Variables and Instance Variables πŸ“

In 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.

php
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 variable

Exploring Class Types πŸ“

As mentioned earlier, PHP supports four types of properties:

  1. Public: Properties with the public keyword can be accessed from anywhere.
php
class ExampleClass { public $publicProperty; } $obj = new ExampleClass(); $obj->publicProperty = 'This is a public property'; echo $obj->publicProperty; // Output: This is a public property
  1. Protected: Properties with the protected keyword can be accessed within the class and its child classes.
php
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 property
  1. Private: Properties with the private keyword can only be accessed within the class in which they are defined.
php
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 property
  1. Static: Properties with the static keyword are class-level properties and can be accessed without creating an instance of the class.
php
class ExampleClass { static public $staticProperty = 'This is a static property'; } echo ExampleClass::$staticProperty; // Output: This is a static property

Quiz πŸ’‘

Quick Quiz
Question 1 of 1

What are PHP properties used for?

Quick Quiz
Question 1 of 1

What is the difference between class variables and instance variables in PHP?