ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is abstract class ? in which scenarios it can be helpful in php ? examples will highly be appreciated .

user-image
تم إضافة السؤال من قبل Muhammad Zeshan , Web Application Developer , Inquiron
تاريخ النشر: 2013/10/09
Muhammad Majid Saleem
من قبل Muhammad Majid Saleem , Senior PHP Developer / Project Manager , SwaamTech

Defination is same from php.net link:

 

Class Abstraction

 

PHP5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

 

When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature. This also applies to constructors as of PHP5.4. Before5.4 constructor signatures could differ.

 

Example is also from php.net but with a little modifications:

 

abstract class AbstractClass{    // Force Extending class to define this method    abstract protected function getValue();    abstract protected function printValue( $toBePrintedValue );    // Common method    public function printOut() {        print $this->getValue() . "\n";    }}

 

class ConcreteClass1 extends AbstractClass{    protected function getValue() {        return "ConcreteClass1";    }    public function printValue( $a, $b ) {

        $c = $a + $b;        return $c;    }}class ConcreteClass2 extends AbstractClass{    public function getValue() {        return "ConcreteClass2";    }    public function printValue( $a, $b ) {

        $c = $a * $b;        return $c;    }}$class1 = new ConcreteClass1;$class1->printOut();echo $class1->printValue(3,4 ) ."\n"; $class2 = new ConcreteClass2;$class2->printOut();echo $class2->prefixValue(3,4 ) ."\n";

 

OUTPUTS

ConcreteClass17 //3 +4ConcreteClass212 //3 *4Note: In simple words, you can override a function in inherited classes.

Shahid Mehmood Khan
من قبل Shahid Mehmood Khan , Systems Support Assistant , SB Group of Companies

visit website for answer. hopefully you will become a satisfactory.

 

http://php.net/manual/en/language.oop5.abstract.php

 

Mohammad Irfan
من قبل Mohammad Irfan , Certified Magento Developer , Xcite.com

An abstract class is a class that contains at least one abstract method, which is a method without any actual code in it, just the name and the parameters, and that has been marked as "abstract".

 

The purpose of this is to provide a kind of template to inherit from and to force the inheriting class to implement the abstract methods.

 

An abstract class thus is something between a regular class and a pure interface. Also interfaces are a special case of abstract classes where ALL 

 

methods are abstract.

 

 

When Used Abstarct Calss

I use abstract classes when I want the inheriting classes to inherit some functionality, and interfaces when I want to set some minimum structural criteria for a group of classes.

 

One thing to remember is that any given class can "inherit" (technically implement) many interfaces but only one sub-class (be that abstract or not).

 

Example:

 

 a PDF document for example will have the same interface as a docx document, and the client code doesn't have to care which object it's handling. Short example (in PHP).

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟