Creating abstract properties in PHP

Hi

I was just wondering if it was possible to create/emulate abstract properties in PHP.

This is for a Page class, which is extended for each page in the site. A skeleton in shown below:


abstract class Page {
    protected $title;

    abstract function displayContent();

    protected function display(){
        // display page, calling displayContent somewhere, and using the title from above
        // loads a page template
    }

    private function __destruct(){
        $this->display();
    }
}

I’d like to make $title abstract as well, ie: it must be set in subclasses. One way is to manually check in the constructor of Page, and then call parent::__constructor() in each subclass, but there must be a better way.

Any ideas?

As far as I know, there would be no way to force a sub-class to implement the abstract property.

Even if you implemented to some logic in the parent class, you would still have to rely on the sub-class calling this method.

You could, possibly, have the parent constructor marked as final to prevent the sub-class overriding it, and have this populate the required property.

Of course, this would require the property to be set at runtime which may not be desired, not to mention limiting any future extensions.

<?php
abstract class Bacon
{
    public $sCookingMethod = null;
    
    final public function __construct($sCookingMethod)
    {
        $this->sCookingMethod = $sCookingMethod;
    }
    
    abstract public function cook();
}
?>
Off Topic:

mmmmm…bacon

You do not need to set $title to abstract in order for the extending class to set it. It is automatically set at the time you extend it. As long as the methods or properties are public or protected they are carried though to the child class.

What about using an interface to force use of a setTitle() and getTitle() methods?


abstract class Page implements Title {
    public $title;
    abstract function displayContent (); 
    function display(){
    echo  "mmmm ...  " . $this->displayContent() . " " . $this->getTitle() ;
    }


    function getTitle(){
    return $this->title ;
    }
} 

interface Title {

    function setTitle() ; 
    function getTitle();

}

class Breakfast extends Page {
function displayContent() {
$this->setTitle();
return 'Fried' ;
}

function setTitle(){
$this->title = "Bacon" ;
}
}

$b = new Breakfast ;
$b->display();

And make the Page class implement the getTitle(), and the concrete implementation must contain a setTitle() method or it throws an error.

setTitle() could easily wait till a title is set at run time like so:

$b->setTitle( “Bacon” );

My aim is to force the extended class to override the property, or the code to stop execution if it doesn’t.

I’d thought of using interfaces, but it seemed like unnecessary function calls, and unnecessarily complex.

I was mainly just interested in whether abstract properties are possible at all.

I’ll probably end up checking the existence in Page::__constructor(), throwing an error if the required properties don’t exist. The parent constructor is already called in each subclass for some initialisation.

Thanks anyway.