English 中文(简体)
Abstract keyword in PHP
原标题:

Hey, I m quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for?

What situations would I use the abstract keyword in? How does it change the class/interface?

最佳回答

(Hope this is simple enough -- I don t think I can do better ^^ )

An abstract class can not be instanciated : you can only create another class that inherits from the abstract class, and instanciate that child class.

And if you declare some methods as abstract, those must be defined in the child class, for that one to be instanciable.

问题回答

Declaring a class abstract means that it must be subclassed in order to be used. An abstract class can not be instantiated. One can see it as an extended interface that might include implementation code (as opposed to an interface).

By declaring a method abstract, you force the sub class to implement the method.

The definition is mentioned above, now I will try to give you an example:

"abstract" ensures that you follow a specific logic, e.g. a ticket s material is ALWAYS "paper", or a creditcard must always have a "code". This is important if you work in a big company which has strict standardisation or if you want to force your developers to follow a specific structure, so their code won t end up in a mess.

    abstract class ticket{

    public function material()
    {
        return  Paper ;
    }

}

abstract class creditcard{

    public function material()
    {
        return  Plastic ;
    }

    abstract function setCode(); // the ";" semicolon is important otherwise it will cause an error

}

class key extends ticket{

    public function getMaterial()
    {
        return parent::material();
    }
}

class anotherKey extends creditcard{

    public function setCode($code)
    {
        $this->code = $code;
    }
}

If we do not define the "setCode" method the parser will return an error on "new anotherKey"

Abstract classes are used to an actual a-kind-of-model relationship. This allows for example a database driver to map the hierarchy, in which aims to provide a common base class, the signatures for the methods of the actual driver classes. The implementation is then carried out in accordance with the predetermined signatures in the actual driver classes.

here is code example

<?php
abstract class AbstrakteKlasse {
  public abstract function methode();
}

class ImplementierendeKlasse extends AbstrakteKlasse {
  public function methode() {
    print "ImplementierendeKlasse::methode() aufgerufen.
";
  }
}

$objekt = new ImplementierendeKlasse;
$objekt->methode();
?>

Though you cannot instantiate an abstract class, you can declare concrete methods/properties/variables (in C#, AFAIK) which will be available to the derived class

class Program
    {
        static void Main(string[] args)
        {
            Dog a = new Dog();
           //concrete properties and methods in abstract base class 
          //are available to derived class
            a.Name = "SuperDog";
            a.EatFood();
            //And now calling Dog s method
            a.Speak();            
            Console.WriteLine(a.GetType());

        }
    }

    public abstract class Animal
    {
        public string Name { get; set; }
        public void EatFood()
        {
            Console.WriteLine("Eating..");
        }
    }

    public class Dog :Animal
    {
        public void Speak()
        {
            Console.WriteLine("Bow .. Bow");
        }
    }




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签