You are here: Re: [PHP] Is their something like $classname::$tablename ? « PHP « IT news, forums, messages
Re: [PHP] Is their something like $classname::$tablename ?

Posted by Andrι Pletschette on 03/15/05 16:35

Thanks Jochem, I now solved the problem with your help, the following way:

In the parent-class I define:

static function getTableName($classname) {
if (defined("$classname::TABLE_NAME")) {
return(constant("$classname::TABLE_NAME"));
} else {
throw new Exception("No TABLE_NAME for ".$classname." defined!");
}
}

And in each child-class I define, the const TABLE_NAME.

Now I have two more problems:
- I have to pass the Classname as a parameter to all my static functions:
for Example: echo testTable::toHtml_table_heading($dbConnect, "testTable");

- I need a similar thing for an Array $fields, which contains the field
descriptions of the class:
static function toHtml_table_heading($dbConnect, $classname) {
$result = "\n\t<tr>";
if (!isset("$classname::$fields")) {
$classname->intializeClass($dbConnect, $classname);
}
....

Thankyou,
AndrΓ©


Jochem Maas wrote:

> AndrΓ© Pletschette wrote:
>
>> Hi,
>>
>> I've got one function getTableName() which returns the Database-Table
>> with the data of a class (see below).
>>
>> As you can see it always calls the static $tablename of the subclass
>> described by $this->classname.
>
>
> I solved this problem by doing the following:
>
> abstract class DBTable
> {
> // we can't define abstract consts.
> // but this must be defined in your subclass
> // const TABLE_NAME = 'xxxxx';
>
> // define this func in your subclasses
> abstract function getTableName();
> }
>
> class C1 extends DBTable
> {
> function getTableName()
> {
> return self::TABLE_NAME;
> }
> }
>
> it seems like a crap way of doing it but
> its very flexible and easy to understand (it just doesn't
> have that certain 'je ne c'est quoi') - none the less it
> was born out of a need for major flexibility (for pretty
> much similar reasons to you - namely generic data objects),
> anyway it gives you many ways to skin the cat (get the tablename):
>
> <?
>
> $c = new C1;
>
> echo $c->getTableName();
> echo C1::getTableName();
> echo C1::TABLE_NAME;
>
> $a = 'C1';
>
> if (defined("$a::TABLE_NAME")) {
> echo constant("$a::TABLE_NAME");
> }
>
> if someone has a better way, bring it on :-) I'd love to be
> able to improve my own code.
>
> You may also want to look at the possibility of using class names
> that are always indentical to the table names - I tried that but
> I didn't like it.
>
>>
>> Thankyou,
>> AndrΓ©
>>
>> Here the function:
>>
>> function getTableName() {
>> if ($this->classname == "class1") {
>> return class1::$tablename;
>> } elseif ($this->classname == "class2") {
>> return class2::$tablename;
>> } elseif ($this->classname == "class3") {
>> return class3::$tablename;
>> }...
>> ...
>> }else {
>> throw new Exception($this->classname." not found!");
>> } }
>>
>
>


--
AndrΓ© Pletschette
GSM: 091 719 991
Skype: pletschette (Internet Telephony)
VCard: http://andre.pletschette.net/
..o°'˜'°o.o°'˜'°o.o°'˜'°o.o°'˜'°o.o°'˜'°o.

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация