note 74802 deleted from function.get-class by peehaa
| From: | peehaa@php.net | Date: | Tue, 29 Nov 2016 10:26:31 +0000 |
| Subject: | note 74802 deleted from function.get-class by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207736@lists.php.net to get a copy of this message | ||
Note Submitter: yicheng zero-four at gmail dot com
----
To matsgefvert dot se and Marc,
I'm sorry but I'm still having a very difficult time understanding why you would want this
behavior. I understand that you are generating some code for some persistent layer for database
access.
In your code example, Marc, you say that you would like the SomeTable::read() to return
"SomeTable" when called from your client. Yet, at this point you *know* what the class
name is. Even assuming you are generating this code automatically, you can simply generate your
SomeTable class as such.
<?php
class PersistedObject {
//...
public static function read($table, $key){
// do some databasey stuff
// with $table which is the table name
return $table;
}
}
class SomeTable extends PersistedObject {
//...
// make a copy in the child class
public static function read($key){
$table = get_class();
return parent::read($table, $key);
}
}
// this will now return "SomeTable"
$someTableObject = SomeTable::read(1234);
?>
I'm still failing to see how this would provide but a convenience for very rare & specific
issues, at the cost of non-OOP behavior by allowing static methods to bind to objects instead of
classes.
One of the most common uses for get_class() is for debugging lines, for example:
<?php
class PersistedObject {
//...
public static function transaction($params){
// do some stuff
if ($error_occurred){
echo "Error : WTFBBQ happened in " . get_class() . "::" . __FUNCTION__ .
". Time to get some coffee!";
}
}
class SomeTable extends PersistedObject {
//...
}
SomeTable::transaction( $someParams );
?>
Now, when an error occurs inside the call to SomeTable::transaction(), we get incorrect error
output.