Pregunta

While I have been developing objects for PHP for some time now, abstract classes are somewhat new to me. From my understanding, I would use an abstract class when I want to define a list of methods I want myself or other developers to follow, while also providing some base methods to build upon.

It appears there is no way to define properties within an abstract class however, which I find odd. Take the following code:

abstract class Gateway {

    abstract public function process();

    public function set_gateway_key( $key ) {

          $this->key = $key;

    }

}

A key of this sort is something you would typically want to restrict access to in order to run through some kind of validation. You could probably imagine some other scenarios as well. However, you are forced to rely on the developer extending this abstract class to set this access modifier.

Am I missing something? Or is there no way to do this?

¿Fue útil?

Solución

Abstract classes can have properties.

abstract class AbstractClass {
    protected $key;

    public function setKey($key) {
        $this->key = $key;
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top