简体   繁体   中英

Using Abstract classes with PHP

I'm new to PHP, and was trying to create an Abstract class with a mix of abstract and non-abstract methods, and then extend the class to implement the abstract methods. The following is portions of my two class files:

<?php
require_once 'Zend/Db/Table/Abstract.php';
abstract class ATableModel extends Zend_Db_Table_Abstract {

    abstract static function mapValues($post);
    abstract static function getTableName();

    public static function newEntry($post)  {  
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $data = mapValues($post, true);
        $db->insert(getTableName(), $data);
        $id = $db->lastInsertId();
        return $id;  
    }

    public static function getEntry($id){
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        return $db->fetchRow("
            SELECT *
            FROM ".getTableName()." 
            WHERE ID = '".(int)$id."'
            "
        );
    }

    public static function editEntry($id,$post) {  
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $data = mapValues($post);
        $db->update(getTableName(), $data, " ID = '".(int)$id."' ");
    }

    public static function deleteEntry($id) {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $db->delete(getTableName()," ID = '".(int)$id."' ");
    }
}
?>

The child class looks as follows:

<?php
require_once 'Zend/Db/Table/Abstract.php';
class Testing extends ATableModel {

    public static function getTableName() 
    {
        return 'TESTING';
    }

    public static function mapValues($post) 
    {
        $data = array (
            'test_description'      => htmlentities($post['testDescription'])
        );
        return $data;
    }
}
?>

Both files are located in the same directory relative to one another. However, when I try to run my application, I get the following error:

Fatal error: Class 'ATableModel' not found in /var/www/testApp/application/models/testing.php on line 20

My guess is that there's something wrong with either the order that I'm loading the files, or with where these files are located, relative to one another. However, I'm not sure how to proceed from here. Suggestions?

You're not including the file with your ATableModel definition.


<?php
// in your test
require_once 'Zend/Db/Table/Abstract.php'; // <- should be the file with ATableModel

In your child class you must include() or require() (or require_once()) the class file you are extending. I'm not familiar with Zend however, and if that framework is supposed to include all files in the same directory, don't know.

Try to add some code to make sure the file containing ATableModel is being included.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM