Yogesh Chauhan's Blog

OOP, Class and Objects Strategies For Beginners (PHP)

in PHP on February 9, 2020

In this article I am going to show you how to create class and objects from that class in details. But before that if you want to just quickly review the OOP concepts, checkout my following article.

I have discussed OOP in PHP in this following article:

A Quick Guide to Object-Oriented Programming in PHP

Let’s start with the situation in a real world.

We have different types of cars in real world. Now a car can have properties like color, brand, model name etc and there are many types of cars as well and there are many types of brands and colors and all. So, if we create a class named Car, their objects can be different types of car for example sedan, SUV etc or different types of brands like Toyota, Tesla etc. Now, if you select brand name as object then the type of car will become a property of the car. For example, Toyota object of Car class has a cat_type property.

Let’s Define A Class

In PHP we use class keyword to define a class and then followed by the name we want to give it to the class and then pair of curly braces (just like PHP functions). All related information like its properties and methods will be inside the curly braces.

Class name is case-sensitive.

Properties are nothing but variables and methods are nothing but functions.

Take a look at the syntax:


class Car{
// properties or variables
....

....

//methods or functions

.....
}

Let’s look at the example. 

Let’s create a class with the name Car and set its properties.


//code inside PHP file

class Car{
  // Define Properties
  public $name;
  public $color;

  // Define Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}

Now, let’s define objects of a Car class.

Now, we have a class named Car, we can have many different types of car. Just change the properties and you’ll have a different car.

But before that we need to create different objects for the class Car and set their properties.

We use new keyword to create new objects.


class Car {
  // Define Properties
  public $name;
  public $color;

  // Define Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
  function set_color($color) {
    $this->color = $color;
  }
  function get_color() {
    return $this->color;
  }
}

$Tesla = new Car();
$Toyota = new Car();
$Tesla->set_name('Tesla Model 3');
$Toyota->set_name('Toyota Camry');
$Tesla->set_color('Black');
$Toyota->set_color('Red');

echo "I have " . $Tesla->get_name() . " and " . $Toyota->get_name() . ".";

echo "<br>";
echo "<br>";

echo $Tesla->get_name() . " has " .  $Tesla->get_color() . " color and " . $Toyota->get_name() . " has " .  $Toyota->get_color() . " color.";

In the example above, I have made two instances of a class Car. One is $Tesla and the other one is $Toyota. After that, I have set names and colors for $Tesla and $Toyota.

Now that everything is set we can call the properties of those objects and print it out using echo or print.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS? #Jan 8 How to Install Xcode Command Line Tools on MacOS?
You might also like these
What are Null Sessions?MiscellaneousHigher Order Functions in JavaScript with ExamplesJavaScriptColumn and Table Alias in PostgresPostgresHow to use [] (square brackets) function in Envision Basic?Envision BasicDebugging in WordPress Part 2: WP_DEBUG_LOG and WP_DEBUG_DISPLAYWordPressCreate a simple bar chart and column chart using pure CSS gridCSS