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 4009026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:49:38+00:00 2026-05-20T08:49:38+00:00

I have good oop understanding but poor understanding of its implementation in php… I

  • 0

I have good oop understanding but poor understanding of its implementation in php…

I have the following code, hope it’s self documented =).

I need to have BB in the output

class A{
    // I can't copy function::classname() to all my descendant classes

    static function classname(){
        echo __CLASS__;
    }
}

class B extends A{

    static function test(){
        self::classname();
    }

    function test1(){
        self::classname();
    }



    //i have A LOT of static and non-static functions using self::classname() in their code
    // I can't copy all them to base class
    }

    $v = new B();
    B::test();
    $v->test1();

I’m stuck with static:: and self:: syntax


PS: another crazy question I’ve come across:

Suppose I have

function doSomething(){
    echo $this->id;
}

Sometimes it gets into the static contexts. Yes, I know, that’s because my bad application design. But is it possible to create a second(mirror, overloading) function

static function doSomething(){
    echo false;
}

It means that using
$obj->doSomething() returns id and using Class::doSomething() returns false


Question 3:

Is it possible to get property default value in static context an property value in non-static context automatically?

  • 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-05-20T08:49:39+00:00Added an answer on May 20, 2026 at 8:49 am

    Have a look at late static binding.

    class A {
        static function classname() {
            echo __CLASS__;
        }
    
        static function test1() {
            static::classname();
        }
    }
    
    class B extends A {
        static function classname() { 
            echo __CLASS__;
        }
    }
    
    $v = new B();
    B::test1();
    $v->test1();
    

    Or as pointed out by Long Ears in the comments, assuming php 5.3.0+ you can use get_called_class()

    class A {
        static function classname() {
            echo get_called_class();
        }
    
        // this can be defined in either class A or B without affecting the output
        static function test1() {
            static::classname();
        }
    }
    
    class B extends A {
    }
    
    $v = new B();
    B::test1();
    $v->test1();
    

    Outputs:

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

Sidebar

Related Questions

I've been studying OOP for quite a while now and I have a good
In my quest in trying to learn more about OOP in PHP. I have
I have a pretty good non-OOP background. I've done lots of Visual Basic coding,
Is it a good OOP practice to have static variables to store global, changing
Specifically I am looking for a good OOP PHP CURL class that can be
I am a seasoned .Net developer and have a good grasp of OOP concepts.
Anybody have good examples of usability disasters? Here's an example. Hector is a manager
I have good knowledge on C++ (not C) and recently bought a Symbian-based phone.
I have good knowledge on the working of a traditional .dll . Also the
Do anyone have good ideas of how to modify the toolbar for the WinForms

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.