Jump to content

How to ... static variables in inherited methods for PHP < 5.3.0 ???


Recommended Posts

 

I find it bizarre that PHP behaves the way it does regarding method definition context and static class variables ($this vs. self).  The self keyword is almost useless; if it's only ever going to refer to the class in which it was defined, why wouldn't I just use the class name explicitly? /rant

 

How can I have a value common to an extended class be correctly referenced in an inherited method?

 

An example case:

 

<?php

abstract class Base {

static protected $table_name = "none";
public function getTableName() {
	return self::$table_name;
}
}

class ChildClass extends Base {
static protected $table_name = "my_table";
}

$cc = new ChildClass;
echo $cc->getTableName(); // outputs "none"
?>

 

I guess I could define an abstract method in the base class that should return the tablename.  What's the standard practice for this situation in PHP?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.