EDDYMENS

Last updated 2023-10-15 09:22:45

What Is A Class In Programming?

Table of contents

Definition

A class is a programming construct, that falls under the object-oriented programming paradigm [↗]. The goal with classes is to represent part of your solution as objects.

Objects in this case are anything that has a behaviour and or data.

Example

Let's represent a car in the form of a class in PHP [↗]. A car has some attributes such as:

  • Color
  • Brand and model
  • Wheel size etc

It can also do a couple of things, such as:

  • Start
  • Accelerate
  • Break
  • Change directions

Etc

01: <?php 02: 03: class Car { 04: 05: public $make = []; 06: 07: public function __construct($brand, $model, $year, $start=true) { 08: $this->make = [ 09: 'brand' => $brand, 10: 'model' => $model, 11: 'year' => $year 12: ]; 13: return $this->startOrStop($start); 14: } 15: 16: public function accelerate() { 17: // acceleration method 18: return $this; 19: } 20: 21: public function break() { 22: // breaking method 23: return $this; 24: } 25: 26: public function moveLeft() { 27: // direction method 28: return $this; 29: } 30: 31: public function moveRight() { 32: // direction method 33: return $this; 34: } 35: 36: public function startOrStop($state) { 37: // ignition method 38: return $this; 39: } 40: 41: }

The above class mimics/represents a few features of a car, like starting accelerating, and moving left or right using methods [→]. The car manufacturing details are stored as a property attribute $make.

Summary

Classes make it easy for developers to create a mental model of large software systems and be able to better visualize the design of systems.

Here is another article you might like 😊 "Diary Of Insights: A Documentation Of My Discoveries"