Open Source Content Management Framework

MgdSchema in PHP - Object constructor

  1. Midgard 8.09
    1. Derived classes
  2. Midgard 1.8
      1. Create "empty" object instance with default properties
      2. Create object instance which should be fetched from database "by id".
      3. Create object instance which should be fetched from database "by guid".

new class_name([mixed])

Midgard 8.09

As of this release midgard-php extension is real language bindings. It means that there is always 1:1 relationship between Zend and GObject objects.

Important change is an exception thrown when new class instance can not be created. So unlike Midgard 1.8 extension, which returned FALSE, constructor in Ragnaroek generation throws __midgard_error_exception__.

try 
{
    $obj = new midgard_article("NOSUCHGUID1234");
}
catch (midgard_error_exception $e)
{
    echo $e->getMessage();
}

midgard_error_exception is generated with Midgard error codes, and it's compatible with midgard_connection::get_error_code() and midgard_connection::get_error_string().

Derived classes

If you extend MgdSchema class, you must always invoke parent constructor in child's one:

class my_own_class extends midgard_article
{
    parent::__construct();
}

In such case, midgard-php extension makes full class introspection and (internally) assign my_own_class object with base class (midgard_article in this example). With this approach, all MgdSchema methods like create, delete, update works on real midgard_article tables. Of course properties not registered for midgard_article can not be taken into account when SQL queries are executed. But you can register as many properties in child class as you need.

Signals are also "inherited", so every derived class' object may connect to callbacks or emit signals, and automatically connects to callbacks (in constructor phase) which were registered with midgard_object_class::connect_default.

Midgard 1.8

Constructor for MgdSchema objects is exactly the same as described in PHP.

Since Midgard version 1.8 , objects' constructor has optional id or guid parameter. When integer parameter is used then object is fetched by id property. When string parameter is used then object is fetched by guid from database.

If passed parameter is integer or string type , then internal function isguid is called to determine if object should be fetched by guid or by id. There is no need to type cast numeric values as such types are internally type casted to integer.Even if they come from argc or REQUEST array.

Object constructor returns FALSE when object's record(s) can not be fetched by particular id or guid ( however type of returned value is Object ). Check for created object's instance may be done with simple control structure:

<?php
$article = new midgard_article("BadGuidWithSome12345");
if(!$article)
    echo "Couldn't create midgard_article object instance!";
?>

E_NOTICE error is logged when other types ( like array or object ) are passed as constructor parameter.

Examples for MgdSchema type midgard_topic.

Create "empty" object instance with default properties

<?php
$object = new midgard_topic();  
?>

Returned $object will have all properties set to their default values.

Create object instance which should be fetched from database "by id".

<?php
$object = new midgard_topic(1);  
?>

This is equivalent to:

 <?php
     $object = new midgard_topic();  
     $object->get_by_id(1);
     ?>

Create object instance which should be fetched from database "by guid".

<?php
$object = new midgard_topic("37cb0d47f718665fd02e3fe16f6c487e");  
?>

This is equivalent to:

 <?php
     $object = new midgard_topic();  
     $object->get_by_guid("37cb0d47f718665fd02e3fe16f6c487e");
     ?>
Designed by Nemein, hosted by Anykey