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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:46:31+00:00 2026-06-11T06:46:31+00:00

Possible Duplicate: Functionality of PHP get_class For a small ORM-ish class-set, I have the

  • 0

Possible Duplicate:
Functionality of PHP get_class

For a small ORM-ish class-set, I have the following:

class Record {
  //Implementation is simplified, details out of scope for this question.
  static public function table() {
    return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', get_class()))."s";
  }

  static public function find($conditions) {
    //... db-selection calls go here.
    var_dump(self::table());
  }
}

class Payment extends Record {
}

class Order extends Record {
  public $id = 12;
  public function payments() {
    $this->payments = Payment::find(array('order_id', $this->id, '='));
  }
}

$order = new Order();
$order->payments();
#=> string(7) "records"

I would expect this code to print:

#=> string(8) "payments"

But, instead, it prints records. I have tried self::table(), but that gives the same result.

Edit, after some questions in the comments table() is a method that simply maps the name of the Class to the table in wich its objects live: Order lives in orders, Payment lives in payments; records does not exist!). When I call Payments::find(), I expect it to search on the table payments, not on the table records, nor on the table orders.

What am I doing wrong? How can I get the classname of the class on which ::is called, instead of the class in which is was defined?

Important part is probably the get_class(), not being able to return the proper classname.

  • 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-11T06:46:33+00:00Added an answer on June 11, 2026 at 6:46 am

    You can use get_called_class if you’re using php 5.3 or higher. It gives you the class the static method is called on, not the one where the method is actually defined.

    UPDATE

    You need the class name of the class on which ‘find’ is called. You can fetch the class name in the find method and provide it as a parameter to the table (maybe rename it to getTableForClass($class)) method. get_called_class will give you the Payment class, the table method derives the table name and returns it:

    class Record {
        //Implementation is simplified, details out of scope for this question.
        static public function getTableForClass($class) {
            return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class))."s";
        }
    
        static public function find($conditions) {
            //... db-selection calls go here.
            $className = get_called_class();
            $tableName = self::getTableForClass($class);
    
            var_dump($tableName);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: HTTP authentication logout via PHP Hi I have a some functionality on
Possible Duplicate: Disable browser 'Save Password' functionality In my application, I have a login
Possible Duplicate: Access variable in class I'm building some caching functionality for a class
Possible Duplicate: Comparing functionality between KeyListeners and Key Bindings I've been trying to use
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: HTML Compress File Upload? I have complains from client that my website
Possible Duplicate: Creating a custom Document Library in SharePoint I have 2 users, Martin
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself
Possible Duplicate: Reference - What does this symbol mean in PHP? Found an image

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.