Back to Developer Roadmap

Properties and Methods

src/data/roadmaps/php/content/properties-and-methods@MRAPXshy9RoYdReY6grf_.md

4.0915 B
Original Source

Properties and Methods

Properties and Methods are fundamental components of Object-Oriented Programming (OOP) in PHP. Properties are just like variables; they hold information that an object will need to use. Methods, on the other hand, are similar to functions; they perform an action on an object's properties. In PHP, properties are declared using visibility keywords (public, protected, or private) followed by a regular variable declaration, while methods are declared like functions but inside a class.

Here is a simple example:

class Car {
  public $color; // Property

  // Method
  public function setColor($color) {
    $this->color = $color;
  }
}

In this example, $color is a property and setColor() is a method.

Visit the following resources to learn more: