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

  • Home
  • SEARCH
  • 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 8989241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:12:32+00:00 2026-06-15T22:12:32+00:00

I have a drawing PHP class called ClassA that is extended by many other

  • 0

I have a drawing PHP class called ClassA that is extended by many other drawing classes, ClassB for instance.

I need the inherited classes to fire its parent classes’ Draw() method. However, in my particular situation, I do not want to call such method directly (e.g.: parent::Draw()). I’d like a third function (e.g.: parent::InvokeDraw()) to call my drawing method from within the parent’s context.

Here’s some code to illustrate:

class ClassA
{
    function Draw()
    {

        /* Drawing code ... */

    }

    function InvokeDraw()
    {
        $this->Draw();
    }
}

class ClassB extends ClassA
{
    function Draw()
    {
        parent::InvokeDraw();

        /* Drawing code ... */

    }
}

The problem I’m facing is that InvokeDraw() will not call the parent’s Draw() method, but rather the extended class’ own Draw() method, thus causing an infinite loop.

Although the issue is fairly logical, I am having a hard time figuring out a workaround on that. How to accomplish this task?

Desired effect

chart

Infinite loop problem

chart

  • 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-15T22:12:33+00:00Added an answer on June 15, 2026 at 10:12 pm

    This one is with using static methods

    <?php
    
    class ClassA
    {
        function Draw()
        {
            echo "DrawA";
            /* Drawing code ... */
    
        }
    
        function InvokeDraw()
        {
            self::Draw();
        }
    }
    
    class ClassB extends ClassA
    {
        function Draw()
        {
            echo "DrawB";
            parent::InvokeDraw();
    
            /* Drawing code ... */
    
        }
    }
    
    echo "Begin:<br>";
    
    $cb = new ClassB();
    $cb->Draw();
    

    Note that the only thing I changed is the InvokeDraw() method and made it use self which refers to a class, rather than object as it is with $this

    Output:

    Begin:
    DrawBDrawA
    

    Edit:
    To answer your comment below I will add a short description of how your code works and how this code works.

    What happens in your code:

    1. We create B and start working with it
    2. We call B->Draw() while working within B class AND B object.
    3. B->Draw() calls statically(that means class method) A::Invoke() from class A BUT we are still using B object.
    4. Static call A::Invoke() calls $this->Draw(); and as we are working currently with B object, $this refers to an instance of ClassB.
    5. And here we go looping.

    What happens in the code above:

    1. We create B and start working with it
    2. We call B->Draw() while working within B class AND B object.
    3. B->Draw() calls statically(that means class method) A::Invoke from class A BUT as well as in your code we are still using B object.
    4. Static call A::Invoke() calls self::Draw() which is basically the same as ClassA::Draw() and because it’s a static method, we don’t care what object we are currently working with and we call the A‘s Draw() method.
    5. A::Draw() method executes as we need.

    I will provide the same explanation for the code in my second answer, which doesn’t use static calls:

    1. We create B and start working with it
    2. We call B->Draw() while working within B class AND B object.
    3. B->Draw() CREATES an instance of A.
    4. B->Draw() calls A->Invoke() which means we start to work with an object that is instance of class A and not B like before.

    At this point we completely forget that B even exists and work only with A

    1. A->Invoke() calls $this->Draw() which means that we are calling A->Draw(), because we already work with an instance of class A.
    2. A->Draw() executes as we expect.

    From usability point of view, we can see that the static method is better, as we can define some static properties and you can work with them when A::Draw() executes. If we use non-static method, then we need to pass the data we need within arguments of our methods.

    I hope this makes it clear. The sequences above do not have the right terminology, but it was written on purpose, I think it’s easier to understand the flow that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody have a PHP (or other language) script for drawing a spiral. A
I have to add drawing facility to my php webpage, so that users can
What I have is a drawing of a UI layout, that I want to
We need to create a program in PHP/MySQL that will display all the 'people',
I have a table that I am drawing information on that has a few
I want to access certain form elements from classes that normally don't have access
this is what i have right now Drawing an RSS feed into the php,
I have a PHP script that creates a very tall image and draws a
At the moment I have a really basic PHP script for randomly drawing quotes
I have realized that my first question won't be answerable, since php can't deal

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.