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

The Archive Base Latest Questions

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

According to php, class::self always points to the class itself, but as I wrote

  • 0

According to php, class::self always points to the class itself, but as I wrote down these codes, something strange happens:

class C_foo{
    function foo() { return "foo() from C_foo"; }
    function bar() { echo self::foo(); }
}

class C_bar extends C_foo{
    function foo() { return "foo() from C_bar"; }
}

C_foo::bar();
C_bar::bar();

I thought the output would have been:

foo() from C_foo
foo() from C_bar

But in fact:

foo() from C_foo
foo() from C_foo

It means that the self in parent class does NOT exactly inherit into the child, it works more like to this:

foo() {return parent::foo();}

Is that a feature from php or is it a bug? Or is it mean to be like this?

Otherwise, such thing is occurred as I tried to tell a class create objects from itself, the code is something like this:

class Models {
    function find($exp) {
        ...
        ...

        $temp_model = new self();
        ...
        ...
    }
}

class Something extends Models {...}

$somethings = Something::find("...");

Maybe someone would ask, “why don’t you set a variable with the value of class, and use the variable as the __construction function?”

Like this:

...
...
function find($exp) {
    ...
    ...
    $class_name = __class__;
    $temp_model = new $class_name();
    ...
    ...
}
...

In fact I did that, and got a even more weird result:

It works only when the class does not have any property or function but find(), or an error telling me a variable shows off where a function sould exist would jump out.

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

    In PHP, classes are not object. Because of that, there is no inheritance of static methods (actually, they are similar to global functions).

    So, when C_foo says self, it always means C_foo (even if you called a method from C_bar).

    If you want create instances from an abstract class method, you should try the Factory pattern.

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

Sidebar

Related Questions

According to the PHP manual , a class like this: abstract class Example {}
I start to learn class in PHP. According to my experience with other language,
According to PHP documentation, HttpResponse is a static class to manipulate the Http response
I'm trying to do something like a bitwise enum in PHP according to this
According to php manual nor php://input neither $HTTP_RAW_POST_DATA work with multipart/form-data POST-requests. php://input allows
According to the PHP manual , in order to make code more portable, they
I would like to change this: // use appropiate lang.xx.php file according to the
According to Secure User Image Upload Capabilities in PHP , the recommended way is
According to http://www.codeguru.com/forum/showthread.php?t=463663 , C#'s getHashCode function in 3.5 is implemented as: public override
According to the PDO documentation , you can store database login details in php.ini,

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.