Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:04:23+00:00 2026-06-13T13:04:23+00:00

here is a code: abstract class A { abstract public function add(); public function

  • 0

here is a code:

abstract class A
{
    abstract public function add();

    public function callItself()
    {
        echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
        if (rand(1,100) == 5) { die; }
        $this->callItself();
    }
}

class B extends A
{
    public function add()
    {
        echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
    }
}

class C extends B
{
    public function add()
    {
        echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
        parent::add();
        parent::callItself();
    }

    public function callItself()
    {
        echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
        throw new Expection('You must not call this method');
    }
}

$a = new C();
$a->add();
die;

in class C the callItself() must not be called, so it drops an exception. I cant set it private as we know 🙂 but at 10 line, the $this->callItself(); calls that method of **C** instead of A so it dies. But I dont want it, how to dodge that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:04:24+00:00Added an answer on June 13, 2026 at 1:04 pm

    Use self::callItself() instead of $this->callItself();

    Replace

    public function callItself()
    {
        echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
        if (rand(1,100) == 5) { die; }
        $this->callItself();
    }
    

    With

    public function callItself() {
        echo __CLASS__ . ':' . __FUNCTION__ . ' (' . __LINE__ . ')<br>';
        if (rand(0, 2) == 0) { <------- Better Probability 
            die();
        }
        self::callItself(); <---- This would continue to call A:callItself until die
    }
    

    Output

    C:add (23)
    B:add (17)
    A:callItself (7)
    A:callItself (7)
    A:callItself (7)
    A:callItself (7)
    A:callItself (7)
    A:callItself (7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the simple php code <? abstract class A{ abstract public function a($x);
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
Here is code example class A{ int i; public: A(int i) : i(i) {}
Consider the following class hierarchy: Abstract class Printer{ public print(){ //code to handle printing
Say we've got the following two classes: abstract class Foo { public abstract function
I have an abstract class method: Public Class Base_BLL { Public Overridable Function Persist(ByRef
<?php abstract class file { private $pid; private $uid; public function __construct($pid,$uid) { $this->pid
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
Here's the code: <?php class Order extends Zend_Db_Table_Abstract { protected $_name = 'orders'; protected
I've got a little problem. Here it is: This is my singleton abstract class:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.