PHP problem with abstract classes and protected functions

1

As the title says, I have a problem with the following code.

abstract class AClass {
    abstract protected function a1();
    abstract protected function a2();

    public function show() {
        return $this->a1() . "<br>" . $this->a2();
    }
}


class A1 extends AClass {

    protected function a1() {
        return 'A1a1';
    }

    protected function a2() {
        return 'A1a2';
    }
}

class A2 extends AClass {

    protected function a1() {
        return 'A2a1';
    }

    protected function a2() {
        return 'A2a2';
    }
}

class AA {

    public function __construct() {
        $a11 = new A1();

        $a22 = new A2();

        $this->inter($a11);
        $this->inter($a22);
    }

    private function inter(AClass $class)  {
        echo $class->show();
    }
}

$aa = new AA();

Return this error:

  

Fatal error: Call to protected A1 :: a1 () from context 'AA' in C: \ xampp \ htdocs \ Learning \ index.php on line 38

Line 38 is this:

$a11 = new A1();

I do not understand why you throw that error in the line if I'm not calling a1 () in it.

Thank you very much already.

Javier

    
asked by Javier Sivack 28.01.2017 в 14:41
source

1 answer

2

Ok, the problem is that I did not know that PHP is not case sensitive, so method a1 is being taken as a constructor.

    
answered by 28.01.2017 в 18:29