Jump to content

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


Wildbug

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.