LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-08-2008, 01:26 PM   #1
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Rep: Reputation: 36
Question PHP5 - OOP heritance and static functions and self::


Hi all!
I'm doing a DB access classes and I pretended to do the statically, except the main BD class, that needs to open and close the database. To do so, I decided to do an abstract class to define the basic per table functions, as basic scape, read, update and remove and then the final classes for each table:

Code:
$db = new database();

abstract class db_table
{
   protected abstract function name();
   protected abstract function escape(array &$data);

   public function read(array $data, array $order = array())
   {
      global $db;
      return $db->read(self::name(), self::escape($data), $order);
   }
   public function insert(array &$data);
   {
      global $db;
       self::escape(&$data);
       $db->insert(self::name(), &$data);
   }
 
   public function update(array &$data);
   {
      global $db;
       self::escape(&$data);
       $db->update(self::name(), &$data);
   }
}

class db_users extends db_table
{
   protected function name()
   {
      return 'users';
   }

   function escape(array &$user)
   {
      ( some mysql_real_escape_string stuff... )

      return $user;
   }
   
   function read_by_id($id)
   {
      $user = self::read(array('id' => $id));

      if(is_array($user))
         return $user;
      
      return NULL;
   }
}

// This will fail
db_users::read(array(id=>'someid',passwd=>'somepassword'));
With the code as here it gives an error saying db_table::name and db_table::escape is not callable on static plus abstract situation. But I'm calling it through users class, which has name and escape functions declared. Is there any possibility to do something similar, without needing of getInstance(), redeclaring the same function on each table class or just not using static classes?

May be using some combination with get_class and eval, or reflecting, or something similar?

If not, I know the solution... ;(
 
Old 07-08-2008, 01:32 PM   #2
excel28
Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 72

Rep: Reputation: 15
Did you try to remove 'self::' from name and escape calls?
 
Old 07-08-2008, 01:37 PM   #3
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Original Poster
Rep: Reputation: 36
It gives a "Call to undefined function name()"...

What I'm looking for is some self:: equivalence but not just for the abstract class but the final one. Just as parent:: is for the final to access the abstract. Or some trick to have the same functionallity.

Thanks for speed, excel!
emi

Last edited by emi_ramo; 07-08-2008 at 01:41 PM.
 
Old 07-08-2008, 07:10 PM   #4
excel28
Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 72

Rep: Reputation: 15
Looking at the first post, calling db_users::read means function "read" is a static function. But looking at your db_table class, it is not a static function.

Try to create an instance of db_users first, then have the object call the function "read".
 
Old 07-08-2008, 08:51 PM   #5
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Currently, PHP binds self at compile-time rather than run-time. So whenever you use self in a class, it always refers to whatever class the code appears in. In your example, the call self::name() in db_table::read() will always resolve to db_table::name(), even though you're calling it from db_user, which overrides name().

I've read that this will be fixed in PHP 5.3, but for now, you're pretty much out of luck. I did find one work-around in the comments on php.net, but it's very ugly. Basically, it involves looping through a backtrace in your parent class method to determine the name of the child class that called the method. When you have that, you can statically execute the method in the child using call_user_func(). Kind of a brilliant hack as a proof-of-concept, but the thought of using it in a production app makes me feel really dirty.

Personally, I would advise you to just bite the bullet and use instance methods. They're nice, clean, work correctly, and don't have the reek of ugly, ugly hackery about them.
 
Old 07-09-2008, 04:28 AM   #6
emi_ramo
Member
 
Registered: Apr 2007
Location: Barcelona, Spain
Distribution: Debian, KUbuntu
Posts: 213

Original Poster
Rep: Reputation: 36
SOLVED!!

Thanks, AdaHacker.
Your info has been much useful. I suppose that I will rewrite (copy-paste) the basic functions until PHP 5.3 will be released.

emi
 
  


Reply

Tags
class, php5, self, static


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP: why OOP over normal functions? konqi Programming 15 05-10-2008 08:10 PM
Php Coding Question: Static Functions joelhop Programming 2 08-09-2006 11:17 AM
static functions in C++ abi_sh Programming 7 07-13-2005 04:09 PM
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
IPC - semphores and static functions Hko Programming 1 01-12-2004 04:55 PM


All times are GMT -5. The time now is 11:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration