AppDividend
Latest Code Tutorials

PHP Class Example | Basics Of Class In PHP Tutorial

0

PHP Class Example | Basics Of Class In PHP Tutorial is today’s topic. Class is a programmer-defined data type, which includes the local functions as well as the local data. You can think of a class as the template for making many instances of the same kind (or class) of the object. OOP is the programming style in which it is necessary to group all of the variables and functions of the particular topic into the single class.

PHP Class Example

In object-oriented programming, a class is a constructor prototype from which objects are created.

The class defines constituent members which enable the class instances to have state and behavior.

Data field members enable the class object to maintain the state and methods enable the class object’s behavior.

#Defining PHP Classes

See the general form of a PHP class.

<?php
   class phpClass {
      var $var1;
      var $var2 = "Game of Thrones";
      
      function myfunc ($arg1, $arg2) {
         // function body
      }
   }

Here is the description of each line.

  1. The particular form class, followed by a name of the class that you wanna define.
  2. The set of braces enclosing any number of variable declarations and function definitions.
  3. Variable declarations start with the particular form var, which is followed by the conventional $ variable name; they may also have the initial assignment to the constant value.
  4. Function definitions look much like the standalone PHP functions but are local to a class and will be used to set the object data and access the object data.

#Creating classes and Instantiation

  1. The class definition starts with a keyword class followed by the class name, then followed by the set of curly braces ({}) which enclose the constants, variables (called “properties”), and functions (called “methods”) belonging to the class.
  2. The valid class name (excluding the reserved words) starts with the letter or underscore, followed by the number of letters, numbers, or underscores.
  3. Class names usually begin with the uppercase letter to distinguish them from other identifiers.
  4. The instance is the object that has been created from an existing class.
  5. Creating the object from an existing class is called instantiating the object.
  6. If we want to create an object out of a class, the new keyword must be used.
  7. Classes should be defined prior to instantiation.

#How to add properties to a class

We call properties to the variables inside the class.

Properties can accept values like the strings, integers, and booleans (true/false values), like any other variable. See the following code.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";
  }

The $name and $favshow are the properties.

We put a public keyword in front of the class property.

The naming convention is to start a property name with the lower case letter.

A property can have a default value. We can also create the property without a default value.

You can access the properties after you create an object of the defined class.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";
  }

  $leo = new Actor;
  echo $leo->name."\n";
  echo $leo->favshow;

See the output.

➜  pro php Actor.php
KRUNAL
Game of Thrones                                                                 
➜  pro

#Defining Methods in Class

The functions which are declared in the class are called methods. The class method is precisely similar to the PHP functions.

Declaring a method in the class is an easy task, use one of a keyword public, protected, or private, followed by the method name.

  1. public: Public method can be accessed from outside the class.
  2. private: It has no access is granted from outside the class.
  3. protected: It has no access is granted from outside a class except the class that’s the child of a class with a protected property or method.

A valid method name starts with a letter or underscores, followed by any number of letters, numbers, or underscores.

A method body enclosed within a pair of braces which contains codes. The opening curly brace ( { ) indicates the beginning of a method code and the closing curly ( } ) brace suggests the termination of the method.

If the method is not defined by a public, protected, or private, then the default is public.

You can access properties and methods of the current instance using $this (Format $this->property) for the nonstatic property. See the code.

<?php
  class Actor {
      public $name = "KRUNAL";
      public $favshow = "Game of Thrones";

      public function show() {
        return $this->favshow;
      }
  }

  $leo = new Actor;
  echo $leo->show();

See the output.

➜  pro php Actor.php
Game of Thrones                                                                 
➜  pro

We can approach the methods similar to a way that we approach the properties, but we first need to create atleast one object from a class.

In this tutorial, we have seen the world of object-oriented programming by learning about the classes and  objects that can be created out of them. 

So, PHP Class Example | Basics Of Class In PHP Tutorial is over.

Recommended Posts

How To Convert PHP Object To Associative Array In PHP

PHP Functions Tutorial With Example | Functions In PHP

PHP Array Pop Example | PHP array_pop() Function Tutorial

PHP array_chunk Example | array_chunk Function Tutorial

PHP Array Push Example | array_push() Tutorial

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.