Only run method in Parent Class not Child Class

  • benwade9721
  • Novice
  • Novice
  • User avatar
  • Posts: 17

Post 3+ Months Ago

Hi everyone,

I am trying to create a class in PHP that is going to be inherited...
The class contains a constructor that I need to call in both classes but I need a way to only run part of the construct once, i.e. run in the parent but not run when the class is extended by a child class...??

Code: [ Select ]
<?
class Parent_Class
{
    function __construct()
    {
        if([color=#FF0000]this is parent class[/color])
        {
            echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
        }
    }
}

class Child_Class extends Parent_Class
{
    function __construct()
    {
        parent::__construct();
    }
}


?>
  1. <?
  2. class Parent_Class
  3. {
  4.     function __construct()
  5.     {
  6.         if([color=#FF0000]this is parent class[/color])
  7.         {
  8.             echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
  9.         }
  10.     }
  11. }
  12. class Child_Class extends Parent_Class
  13. {
  14.     function __construct()
  15.     {
  16.         parent::__construct();
  17.     }
  18. }
  19. ?>


Does anyone have any ideas?
  • righteous_trespasser
  • Scuffle
  • Genius
  • User avatar
  • Posts: 6229
  • Loc: South-Africa

Post 3+ Months Ago

benwade9721 wrote:
Hi everyone,

I am trying to create a class in PHP that is going to be inherited...
The class contains a constructor that I need to call in both classes but I need a way to only run part of the construct once, i.e. run in the parent but not run when the class is extended by a child class...??

Code: [ Select ]
<?
class Parent_Class
{
    function __construct()
    {
        if([color=#FF0000]this is parent class[/color])
        {
            echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
        }
    }
}

class Child_Class extends Parent_Class
{
    function __construct()
    {
        parent::__construct();
    }
}


?>
  1. <?
  2. class Parent_Class
  3. {
  4.     function __construct()
  5.     {
  6.         if([color=#FF0000]this is parent class[/color])
  7.         {
  8.             echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
  9.         }
  10.     }
  11. }
  12. class Child_Class extends Parent_Class
  13. {
  14.     function __construct()
  15.     {
  16.         parent::__construct();
  17.     }
  18. }
  19. ?>


Does anyone have any ideas?

Maybe something like this:

Code: [ Select ]
<?
class Parent_Class
{
    function __construct($child = false)
    {
        if($child == false)
        {
            echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
        }
    }
}

class Child_Class extends Parent_Class
{
    function __construct()
    {
        parent::__construct(true);
    }
}


?>
  1. <?
  2. class Parent_Class
  3. {
  4.     function __construct($child = false)
  5.     {
  6.         if($child == false)
  7.         {
  8.             echo 'This is echoed from the parent class, and not echoed when this class is extended by another class';
  9.         }
  10.     }
  11. }
  12. class Child_Class extends Parent_Class
  13. {
  14.     function __construct()
  15.     {
  16.         parent::__construct(true);
  17.     }
  18. }
  19. ?>
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

If you want something conditional / flexible, I'd probably go with what RT posted. If you simply want to prevent extending classes from calling the base classes constructor, there's this.

PHP Code: [ Select ]
<?php
 
class base
{
   public function __construct()
   {
      if(get_class($this) != __CLASS__) return;
 
      echo 'base::__construct()<br>', PHP_EOL;
   }
}
 
class extension extends base
{
   public function __construct()
   {
      echo 'extension::__construct()<br>', PHP_EOL;
      parent::__construct();
   }
}
 
$obj = new base();
echo '<hr>', PHP_EOL;
$obj = new extension();
 
?>
  1. <?php
  2.  
  3. class base
  4. {
  5.    public function __construct()
  6.    {
  7.       if(get_class($this) != __CLASS__) return;
  8.  
  9.       echo 'base::__construct()<br>', PHP_EOL;
  10.    }
  11. }
  12.  
  13. class extension extends base
  14. {
  15.    public function __construct()
  16.    {
  17.       echo 'extension::__construct()<br>', PHP_EOL;
  18.       parent::__construct();
  19.    }
  20. }
  21.  
  22. $obj = new base();
  23. echo '<hr>', PHP_EOL;
  24. $obj = new extension();
  25.  
  26. ?>


Comment out the following line if you want to see both constructors called the way it usually works.

PHP Code: [ Select ]
if(get_class($this) != __CLASS__) return;
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

What you might also consider doing is removing initialization code from the constructor and using a factory? That way gives you far more control over the construction of the class as opposed to having it stiched in if else statements

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 52 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.