Hot File

How to Abstract Class in PHP

View: 194    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Php&mySql   Fields: Other

0 point/0 review File has been tested

What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

Introduction

What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

Abstract Class in PHP

An abstract class is a class that contains at least one abstract method. An abstract method is simply a function definition without any actual code in it. An abstract method contains a method name and the parameters of a method with an abstract keyword at the beginning of a method.

An Abstract class acts as a blue print for a class which inherit them. A child class which inherits an abstract class needs to implement their abstract methods. An abstract class is only created for an inheritance, it means you can’t create their object directly.

Important Points about an Abstract Class

1. An Abstract Class cannot be instantiated, It means you cannot use them directly.

2. An abstract method should not be private .

3. When any child class inherits an abstract class then all the abstract methods in a parent class must be defined in a child class

4.  An Abstract class can also have non-abstract (concrete) methods.

5. An Abstract class does not support multiple inheritance.

Interfaces in PHP

When to use an Abstract Classes

The real question is when to use an abstract classes in a code.

Use an abstract class, when you need to implement a common functionality across multiple classes. In this way, your code is organized and well structured.

To understand this concept, let’s create an abstract class car. Every car has their own model type such as BMW, Swift, HondaCity etc. which they need to define. And each car shares some common functionality such as all car have four wheels.

Let’s create a class car. In a car class, I’ll create an abstract method modelType.

Every car has four wheels, so let’s define a concrete method wheelcount(). Every child class which inherits car class must define their own model type

abstract class car{
 
   /* Abstract method, this must be define in child class. */
 
    abstract public function modelType(); 
 
   /* Concrete method, which is same in all classes. */
 
    public function wheelCount() {
 
      echo "I have four wheels";
    }
 
}

You can’t create an object of an abstract class directly. Let’s check what happens, if you try to create an object.

$data = new car;

PHP Fatal error: Cannot instantiate abstract class. So remember you can’t instantiate abstract classes.

Let’s create a child class, which inherits an abstract class.

class HondaCity extends car{
 
    public function modelType(){
        echo "HondaCity";
    }
}
 
$car = new HondaCity;
$car->modelType();
/* Common method, which can be accessed using a child class object. */
$car->wheelCount();

class Swift extends car {
 
 public function modelType() {
 echo "swift";
 }
}
 
$car = new Swift;
$car->modelType();
$car->wheelCount();
​Example 2  – Let’s take an another example to understand an abstract class
//An abstract class shape
 
abstract class Shape {
    
    abstract protected function getColor();
 
    abstract protected function setColor($color); 
   
    //Common method available for all the classes which extends shape class
    public function describe() {
        return sprintf("I'm an %s %s\n", $this->getColor(), get_class($this));
    }   
}

Let’s create a circle class which extends an abstract class shape

// Class square extends shape class
 
class Square extends Shape {
    
    private $color = null;
    
 
    public function getColor() {
        return $this->color;
    }   
    
    public function setColor($color) {
        $this->color = $color;
    }   
}
 
//Instantiate the Square class
 
$square = new Square();
 
//Set the color
$square->setColor('Orange');
 
// Print the description
echo $square->describe();

For more information, you can check php.net documentation.

Conclusion

By using an abstract class in your code, you can provide a blueprint for a child classes.

How to Abstract Class in PHP

How to Abstract Class in PHP Posted on 07-10-2016  What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation. 5/10 194

Comment:

To comment you must be logged in members.

Files with category

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 1735    Download: 0   Comment: 0   Author: none  

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    Category: Php&mySql
    Fields: Other

    5/1 review
    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    View: 206    Download: 0   Comment: 0   Author: none  

    How to MySqli Tutorial PHP MySqli Extension

    Category: Php&mySql
    Fields: Other

    0/0 review
    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    View: 186    Download: 0   Comment: 0   Author: none  

    Make Laravel Artisan Commands

    Category: Php&mySql
    Fields: Other

    0/0 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

  • Check if a Number is a Power of 2

    View: 165    Download: 0   Comment: 0   Author: none  

    Check if a Number is a Power of 2

    Category: Php&mySql
    Fields: Other

    2.25/2 review
    How to check if a number is a power of 2. To understand this question, let’s take some example.

  • Concatenate columns in MySql

    View: 225    Download: 0   Comment: 0   Author: none  

    Concatenate columns in MySql

    Category: Php&mySql
    Fields: Other

    0/1 review
    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow

  • How to Query NULL Value in MySql

    View: 159    Download: 0   Comment: 0   Author: none  

    How to Query NULL Value in MySql

    Category: Php&mySql
    Fields: Other

    5/1 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • How to Abstract Class in PHP

    View: 194    Download: 0   Comment: 0   Author: none  

    How to Abstract Class in PHP

    Category: Php&mySql
    Fields: Other

    0/0 review
    What is an abstract class in PHP and when to use an abstract class in your application. In this tutorial, we’ll learn about abstract class and their implementation.

  • Use Enums in Rails for Mapped Values

    View: 188    Download: 0   Comment: 0   Author: none  

    Use Enums in Rails for Mapped Values

    Category: Php&mySql
    Fields: Other

    5/1 review
    When I worked in a call center, we used to mark cases with different statuses. This allowed upper management to get a handle on where cases stood, what the bottlenecks were and flow of calls. Thankfully it has been a long time since I worked in a...

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd