Pages - পৃষ্ঠাসমূহ

Object oriented php tutorial - class and object

Object-oriented programming is a style of coding where a large project can complete simple way with less coding. When a class is created under a project then it call again and again. This post show the tutorial of basic Object Oriented Php (OOP). After creating a class it can be called again and again and do less coding after creating a class. So OOP is most importatnt and useful. If you want to shitch from PHP to Objected Oriented PHP, let's start to learn.
Hide Example Show Example

Object Oriented PHP ( OOP )


OOP : Object-oriented programming is a style of coding that allows developers to group similar tasks into classes. This model organized around "objects" rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.OOP was first invented for the purpose of physical modelling in the Simula-67. Object Oriented Programming (OOP) is a programming concept that treats functions and data as objects.Object-oriented programming (OOP) is a programming paradigm using "objects".Object Oriented Programming techniques may include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance. Many modern programming languages now support OOP.

The concepts and rules used in object-oriented programming provide these important benefits:

  • The concept of a data class makes it possible to define subclasses of data objects that share some or all of the main class characteristics. Called inheritance, this property of OOP forces a more thorough data analysis, reduces development time, and ensures more accurate coding.
  • Since a class defines only the data it needs to be concerned with, when an instance of that class (an object) is run, the code will not be able to accidentally access other program data. This characteristic of data hiding provides greater system security and avoids unintended data corruption.
  • The definition of a class is reuseable not only by the program for which it is initially created but also by other object-oriented programs (and, for this reason, can be more easily distributed for use in networks).
  • The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.


PHP started Object oriented Programming.Starting with PHP 5, the object model was rewritten to allow for better performance and more features. This was a major change from PHP 4. PHP 5 has a full object model.Among the features in PHP 5 are the inclusions of visibility, abstract and final classes and methods, additional magic methods, interfaces, cloning and typehinting. PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.

This tutorial contains some parts of tuorial

Class, class properties and object in php
Interface and abstractor in php
Constructor, destructor and function overloading in php
Inheritance in php
Constant, properties, functions and Visivility in php
static, $this,final keyword,Scope Resolution Operator (::) in php
some magic words in php


Defination of Class and Object


Object : Simply put, an oject is a bunch of variables and functions all lumped into a single entity. The object can then be called rather than calling the variables or functions themselves. Within an object there are methods and properties. The methods are functions that manipulate data withing the object. The properties are variables that hold information about the object.

Class : A class is the blueecho for your object. The class contains the methods and properties, or the charactaristics of the object. It defines the object. Lets just start with some examples to see how it all pieces together. We will use a vehicle as our object. All vehicles share similar charactaristics, eg: number of doors, they are painted some color, they each have a price. All vehicles do similar things also, drive, turn left, turn right, stop etc. These can be described as functions, or in OOP parlance, methods. So, the class holds the definition, and the object holds the value. You declare class in PHP by using the class keyword.

Class and object declare in php

In object Oriented php coding style is structure way. Some php keywords are same.Now see the how to start php script.
<?php
 
?>  

This tags define that inside any content in the tag is php script. Otherwise can not detect php script or do not work the script. Now create a class in the php script.
Syntax of the Class declare in php.
<?php
 class vehicle{
  //class content goes here
 }
?>  
Like php, in Object oriented php can comment same way. See this two way.

<?php
 class vehicle{
  //singla line comment
  /* multiple
  line comment */
 }
?>  
After creating a class now we learn which and what a class content and how to define them. In a class there has variable and functions which are called prperties in php. Now declare a Class with class properties
<?php
 class vehicle{
  public $brand;
  private $color;
  protected  $price;
  public $as = <<<EOD
     hello world
     EOD;
 
  function shop(){
   echo "Welcome to car vehicle shop.";
  }
  
  public function showDetails(){
   echo "Details of vehilce Information";
  }
  
  private function showShape(){  
   echo "Shapes of the car.";
  } 
 }
?> 
To declare the properties or value of the properties,ther has some rules. See the below example to check some valid properties
<?php
 class SimpleClass{
    // invalid property declarations:
    public $var1 = 'hello ' . 'world';
    public $var2 = <<<EOD
      hello world
      EOD;
    public $var3 = 1+2;
    public $var4 = self::myStaticMethod();
    public $var5 = $myVar;

    // valid property declarations:
    public $var6 = myConstant;
    public $var7 = array(true, false);

    // This is allowed only in PHP 5.3.0 and later.
    public $var8 = <<<'EOD'
      hello world
      EOD;
 }
?>
Here all variables and the functions are class properties.

Now create a instiated of a class is a simple task. Create an object from class
<?php
 class vehicle{
  public $brand;
  private $color;
  protected  $price
 
  function shop(){
   echo "Welcome to car vehicle shop.";
  }
  
  public function showDetails(){
   echo "Details of vehilce Information";
  }
  
  private function showShape(){  
   echo "Shapes of the car.";
  } 
 }
 
 
 $object= new vehicle;   //create instiatate or object
 
 echo $object->shop();
 echo $object->showDetails();
 
?> 
From the above class there we find some keywords like public, private, protected. They are called for any class properties to acess other thins.

 
 public : this keyword define any variable or function is public.
 private : this keyword define any variable or function is private.
 protected : this keyword define any variable or function is private.


1 comment:

  1. PHP tutorials I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

    ReplyDelete