SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    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?

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php
    abstract class Bacon
    {
        public 
    $sCookingMethod null;
        
        final public function 
    __construct($sCookingMethod)
        {
            
    $this->sCookingMethod $sCookingMethod;
        }
        
        abstract public function 
    cook();
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    mmmmm....bacon

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    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.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What about using an interface to force use of a setTitle() and getTitle() methods?

    PHP Code:
    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" );

  6. #6
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    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.
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •