Class Common Terminologies: OOPs in PHP

easy solution web : OOPS in PHP

In this section, we will understand common terminologies in class like:-

  1. Member Variables
  2. Members Functions
  3. Class Constant
  4. Static Variable and functions.
  5. Access Specifiers

These are the common terms we must know to understand the OOPs concepts in details. We already have a little bit of understanding of Member Variables and Member Functions.Let’s discuss in details.

Member Variables

Properties of the class are defined as Member Variables which is also known as attributes. For Example

If there is a class called “Person”

Now there will also be some properties or attributes which defines the class more clearly.

For example

Class and Attributes : Person
Class and Attributes: Person

Attributes of Person Class can be much more it depends on you what level of definition you want

While creating an Object of Person Class you will need to define the value of these attributes which will be a person object.

If we see in real-world context, If we define the value these we can identify some person by defining the attributes. So every person in this world is an object of Person Class. That is why sometimes why defining Object we call it a real-world entity of a Class.

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
}

Above code explains how to define Member Variables in a Class.

How to access Member Variables through an Object of A Person

$perObj = new Person();  // Syntax of creation of object from a class
$perObj->name = "Mike";
$perObj->gender = "Male";
$perObj->address = "b- block 123 street";

Member Functions

Member functions are defined as methods defined inside a class which let you manipulate the input provided to that function and provides you some output. It is also able to manipulate the member variables defined in the class.

Additionally, Member function exactly tells the behavior of the class or defines that what an Object will be able to do.

According to our example :

What will a Person Object be able to do?

Our Person Object will be able to

walk()

eat()

run()

see()

talk()

…..

So how can we define this behavior in Class

class Person{ 
public $name; 
public $gender; 
public $address; 
public $height; 
public $color; 
public $race;
public function walk(){
echo "person is walking";
} 
public function eat(){
echo "person is eating";
} 
public function run(){
echo "person is running";
} 
}

I have defined the three only, you can define others also.

How to access these Member function through the Object.

$perObj = new Person();
$perObj->walk();
$perObj->eat();
$perObj->run();

Class Constant

This is defined as the properties or attributes of the class which will never change. So once we define a constant it will never be going to be changed in the Object Life Cycle.

So if for a class we need a variable which will be constant throughout the Objects Like Every Person is a Human. Human value is never going to change in any condition.

Syntax to define a constant in a Class

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
constant SPECIES = "Homo Sapiens";
}

By default Constant is public but you can change it according to your need.

To access the constant within the class we use:-

echo self::SPECIES;  // use self to access a constant within the class using ::

To access the constant outside the class we use:-

echo Person::SPECIES;  // use class name to access a constant outside the class using operator ::

Static Variable and Functions

This is defined as properties or functions which is sharable between the objects of the Class. But it can be changed anytime and if it changed then its value for every Object will also be changed.

Memory Allocated to static variables or functions happens to be one time, no matter how many times we have created the object from the class.

Where we need this type of variables: If students from the same college, having ID card in which have college name as “ABC”. Now It somehow changed to “EFG” so now each student college name should changes to “EFG” from “ABC” So If Student is a class then the college_name will a static variable.

Syntax to define Static Variable inside the class :

class Person{
public $name;
public $gender;
public $address;
public $height;
public $color;
public $race;
public static $species = "Homo Sapiens";
public static function getSpecies(){
return self::species;
} 
}

This is how we define a Static Member Variables and Member function inside the class.

How to access static variables and function outside the class

echo Person::species;
echo Person::getSpecies();

calling of static variable and function is same as we do with Class Constant using the class name with ::

$this is not available with the static function. So inside the static function, you can only static variable not the member variable which has been explained above.

Access Specifiers or Visibility

Visibility of a variable or functions set the rules to what extent you are able to access that variable or function.

We have three types of Access Specifiers or Visibility in PHP

  1. Public
  2. Private
  3. Protected

For example

In class, if you have defined a variable or function global than it is accessible to anywhere throughout the code with the help of object outside the class.

But if defined that variable as private then it will not be accessible to anywhere except inside the class. Private variables or function can be used inside the class only using $this.

Now if you want only child class of a parent class able to use a variable or function and the parent class itself, not others than you define such variables as protected. In protected only parent and child, class will be able to use that variable or function.

Syntax to define variables and function with access specifiers inside the class:

public $name // $name is defined as public. It means name variable is accessible outside the class with the help of Object of the Class
private $name // $name is defined as private. It means name variable is accessible inside the class only with the help of $this
protected $name // $name is defined as protected. It means name variable is accessible in the parent class and in those child class who extend the parent class.