GObject::register_type

boolregister_type(stringclassname);

Allows custom signals and properties to be used in PHP classes.

You must call this method before attempting to instantiate any classes that use custom signals and properties.

This method is static.

Example 1. Creating Custom Signals

<?php
/* Extend GObject or a child of GObject */
class NewType extends GtkButton {
    /* Define our signals.  The format is signal name => array(when to run, return type, array(parameters required))
    NOTE: this MUST be PUBLIC - after instatiation php-gtk will unset this property for you, so it will not exist
    in your object after creation */
    public $__gsignals = array(
            /* override means we are overwriting the default handler provided by gtk for gtkbutton */
            'clicked' => 'override',
            'mashed'  => array(GObject::SIGNAL_RUN_LAST, GObject::TYPE_BOOLEAN, array(GObject::TYPE_LONG, GtkRequisition::gtype)),
            );

    /* any method named ___do_{$signalname} will be considered the default handler for a signal */
    public function __do_clicked() {
        echo "do_clicked called\n";
    }

    function __do_mashed($arg, $arg2) {
            echo "NewType: class closure for `mashed` called with arguments {$arg}, ", get_class($arg2), "\n";
    }
}

/* handler to attach to our new signal */
function my_handler($obj, $arg, $arg2, $ex) {
    echo "my_handler called with args {$arg}, ", get_class($arg2), " and extra {$ex}\n";
    /* This stops bubbling */
    return true;
}

GObject::register_type('NewType');
$c = new NewType;
$r = $c->size_request();
$c->connect('mashed', 'my_handler', 99);
$c->emit('clicked');
var_dump($c->emit('mashed', 42, $r));

/* Returns:
do_clicked called
my_handler called with args 42, GtkRequisition and extra 99
NewType: class closure for `mashed` called with arguments 42, GtkRequisition
bool(false)
*/
?>

Example 2. Creating Custom Properties

<?php
/* Extend GObject or a child of GObject */
class NewType extends GtkLabel {
    /* Define our properties.  The format is property name => array(property type, nick name for the property,
      description of the property, how the property can be accessed, default property value)
      If you use default values, they must match the type assigned to the property
    NOTE: this MUST be PUBLIC - after instatiation php-gtk will unset this property for you, so it will not exist
    in your object after creation */
    public $__gproperties = array(
            'foo' => array(GObject::TYPE_STRING,  'foo property', 'new foo property', GObject::PARAM_READWRITE, 'default foo value'),
            'bar' => array(GObject::TYPE_OBJECT,  'bar property', 'new bar property', GObject::PARAM_READWRITE),
            'zoo' => array(GObject::TYPE_BOOLEAN, 'zoo property', 'new zoo property', GObject::PARAM_READABLE, 0),
            );
    private $foo;
    private $bar;
    private $zoo = 1;

    function __construct()
    {
            parent::__construct();
            $this->foo = 'abcdef';
    }

    function __get_gproperty($spec)
    {
            echo "__get_gproperty called for $spec\n";
            if ($spec->name == 'foo') {
                    return $this->foo;
            } else if ($spec->name == 'bar') {
                    return $this->bar;
            } else if ($spec->name == 'zoo') {
                    return $this->zoo;
            } else {
                    trigger_error('Unknown property');
            }
    }

    function __set_gproperty($spec, $value)
    {
            echo "__set_gproperty called for $spec = $value\n";
            if ($spec->name == 'foo') {
                    $this->foo = $value;
            } else if ($spec->name == 'bar') {
                    $this->bar = $value;
            } else {
                    trigger_error('Unknown property');
            }
    }
}

GObject::register_type('NewType');
echo $c, "\n";
$c = new NewType;
var_dump($c->get_property('foo'));
var_dump($c->get_property('bar'));
var_dump($c->get_property('zoo'));
$c->set_property('bar', new GtkButton());
echo $c->get_property('bar');
echo "\n";

/* Returns:
__get_gproperty called for [GParamString 'foo']
string(6) "abcdef"
__get_gproperty called for [GParamObject 'bar']
NULL
__get_gproperty called for [GParamBoolean 'zoo']
bool(true)
__set_gproperty called for [GParamObject 'bar'] = [GtkButton object (GtkButton Gtk+ type)]
__get_gproperty called for [GParamObject 'bar']
[GtkButton object (GtkButton Gtk+ type)]

*/
?>