Добро пожаловать на форум PHP программистов!
За последние 24 часа нас посетил 17101 программист и 1835 роботов. Сейчас ищут 1514 программистов ...

Object

Вернуться к: MDB_QueryTool

Object

Object – An introduction to the usage of Objects with MDB_QueryTool

It is possible to use objects as result. A comprehensive example may be seen in the intro.

But using objects is not a simple alternative to using arrays as result. It is also possible to register a custom Class to be returned instead of the default MDB_QueryTool_Result_Row. The new resulting class has to be child of the MDB_QueryTool_Result_Row class.

Example 1 - Change the default result class

To change the resulting class the method setReturnClass is used.

<?php
require_once 'MDB/QueryTool.php';
require_once 
'MDB/QueryTool/Result/Object.php';
define('TABLE_CARS''cars');
$dsn 'mysql://user:pass@host/dbname';

/**
 * Let's suppose the "cars" table has the following fields:
 * (id, model, hp, color, clima, price)
 */
class Car extends MDB_QueryTool
{
    public 
$table TABLE_CARS;
}

class 
CarEntity extends MDB_QueryTool_Result_Row
{
}

//instanciate an object of the Car class
$car = new Car($dsn);
$car->useResult('object');
$car->setReturnClass('CarEntity');
?>

Controlling class variable access

This can now be used to implement getter and setter and thus regulate the accessability to the values. In order to block the access to the class variables from outside they have to be declared as protected. Declaring them as private would result in also blocking the parent class, which gets the data, of accessing them. Of course when doing so corresponding methods have to be implemented to access the variables again.

Example 2 - Restricting class variable access

To keep the example short only methods for dealing with model, hp and clima have been implemented.

<?php
require_once 'MDB/QueryTool.php';
require_once 
'MDB/QueryTool/Result/Object.php';
define('TABLE_CARS''cars');
$dsn 'mysql://user:pass@host/dbname';

/**
 * Let's suppose the "cars" table has the following fields:
 * (id, model, hp, color, clima, price)
 */
class Car extends MDB_QueryTool
{
    public 
$table TABLE_CARS;
}

class 
CarEntity extends MDB_QueryTool_Result_Row
{
    protected 
$id;
    protected 
$model;
    protected 
$hp;
    protected 
$color;
    protected 
$clima;
    protected 
$price;
    public function 
getModel() {
        return 
$this->model;
    }
    public function 
setModel($model) {
        
$this->model $model;
    }
    public function 
getHp() {
        return 
$this->hp;
    }
    public function 
setHp($hp) {
        
$this->hp $hp;
    }
        public function 
getClima() {
        if (
$this->clima) {
            return 
true;
        } else {
            return 
false;
        }
    }
    public function 
setClima($clima) {
        if (
$clima) {
            
$this->clima 1;
        } else {
            
$this->clima 0;
        }
    }
}

//instantiate an object of the Car class
$car = new Car($dsn);
$car->useResult('object');
$car->setReturnClass('CarEntity');
?>

Outlook

This example only demonstrates a very basic feature. But it enables for example the implementation of Decorators. Every feature of modern OOP is now possible to implement.



Вернуться к: MDB_QueryTool

© 2024 «PHP.RU — Сообщество PHP-Программистов»
Главная | Форум | Реклама на сайте | Контакты VIP Сувениры
Разработка компании ODware