www.webdeveloper.com
Results 1 to 5 of 5

Thread: Difference between abstract class and class with protected variables

  1. #1
    Join Date
    Jul 2014
    Posts
    3

    Difference between abstract class and class with protected variables

    Newbie here.

    Abstract classes are special because they can never be instantiated. You typically inherit a set of base functionality from them in a new children class. They are commonly used as the base classes in a larger class hierarchy.

    When creating a non abstract class with protected variables and functions it can also never be instantiated.

    So in which case you will use an abstract class as opposed to a class with protected variables and functions? Is it the same thing?

    Thanks in advanced.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    Quote Originally Posted by ricardodeevelyn View Post
    ...
    When creating a non abstract class with protected variables and functions it can also never be instantiated.
    ...
    Err...no. Where'd you get that idea? Protected (or private) just controls the visibility of those attributes from outside of the class, or from related classes. See http://www.php.net/manual/en/languag...visibility.php
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2014
    Posts
    3
    So the following class can be instantiated? It will not throw any errors?

    PHP Code:
    $NewClass = new MyClass();

    class  
    MyClass
    {

        protected 
    $var1 10;
        protected 
    $var2 20;
        protected 
    $var3 30;

        protected function 
    __construct()

        {
          echo 
    1;
        }



  4. #4
    Join Date
    Jul 2014
    Posts
    3
    I get the following when trying to instantiate that class:

    Fatal error: Call to protected MyClass::__construct() from invalid context

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,359
    Ah, it will throw an error, because "new" will try to call the __construct() method, but since that method is not public, it cannot be called from outside of the class. Normally you'll want __construct() (and __destruct()) to be public, one exception being when you create a Singleton class, in which case you have a separate public static method that calls the constructor, e.g.:
    PHP Code:
    <?php

    // $NewClass = new MyClass();
    $newClass MyClass::getInstance();

    class  
    MyClass
    {
        protected static 
    $instance;

        protected 
    $var1 10;
        protected 
    $var2 20;
        protected 
    $var3 30;

        protected function 
    __construct()
        {
          echo 
    1;
        }

        public static function 
    getInstance()
        {
            if(empty(
    self::$instance)) {
                
    $className __CLASS__;
                
    self::$instance = new $className();
            }
            return 
    self::$instance;
        }
    }
    But like I said: that's an exception to the rule. Normally __construct() should be public.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles