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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:55:04+00:00 2026-05-22T21:55:04+00:00

Say I’ve got the following: <?php abstract class MyParent { public static $table_name; public

  • 0

Say I’ve got the following:

<?php
abstract class MyParent
{
    public static $table_name;

    public static get_all(){
        return query("SELECT * FROM {$this->table_name}");
    }

    public static get_all2(){
        return query("SELECT * FROM ".self::table_name);
    }
}

class Child extends MyParent
{   public static $table_name = 'child'; }
?>

Assuming that query is correctly defined, neither of these methods does what I want: get_all() throws Fatal error: Using $this when not in object context in /path/to/foo.php on line xx because $this is an instance variable.

and get_all2() throws Fatal error: Undefined class constant 'table_name' in /path/to/foo.php on line xx because self is statically determined.

It seems like this kind of thing is the whole point of inheritance, so it should be possible at least easily, if not elegantly. (This is PHP after all.)

What should I be doing?

  • 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-22T21:55:05+00:00Added an answer on May 22, 2026 at 9:55 pm

    You need to change self::table_name to self::$table_name – note the dollar sign. But the best way is to use PHP 5.3’s static keyword:

    http://php.net/manual/en/language.oop5.late-static-bindings.php

    The self keyword references only the class that static proparty was defined, so it is wrong in this case, as you need to get static property ‘inherited’ from parent class. The keyword ‘static’ in this case will resolve correct caller class and work correctly.

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

Sidebar

Related Questions

Say I have the following: class Base { public Base (int n) { }
Say I have a class named Frog, it looks like: public class Frog {
Say Suppose you have a class public class Person { public int PesronId{get;set;} public
Say we have the following method: private MyObject foo = new MyObject(); // and
Say I have the following file structure: app/ app.py controllers/ __init__.py project.py plugin.py If
Say I've got this array: MyArray(0)=aaa MyArray(1)=bbb MyArray(2)=aaa Is there a .net function which
Say I have a Student table, it's got an int ID. I have a
Say I have a class called Money which has parameters Dollars and Cents I
Say we have the following simple data-frame of date-value pairs, where some dates are
Say I have a class with synchronized methods. It's actually a class that uses

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.