01 May 2013 - After the site upgrade, all passwords were reset and you will need to ask the site for a login reset on your first connection.

Callback function and object oriented php style

Dear Php-Gtk user and believers,

Yesterday I found something out about php-gtk and object oriented stuff and I guess a lot of people will come to the same trouble : when you connect signals from your class you have to watch out e.g.

<?php
$treeview
->connect('drag-data-received', array($this,'on_drop'));
?>
  • $this without ' ' means the instance of the class
  • '$this' so with quotes it means the class itself

so most of the time you will want to have the instance of the class because you want to get acces to some data of your instance.

Thanks to cweiske.de I found the solution. and I didn't find an other site which stipulates this really good. So I hope people will read this blog and won't struggle with the same problem again.

Thankx

Comentários

Not true

$this without the single quotes '' references the object instance so the callback will do the same as if you do $this->function();
'$this' means $this literaly not even (string)$this, its just like "\$this" so if you pass '$this' as the class the callback will call '$this'::function() and that is just wrong.

So if you pass a string (like '$this') as the 0 offset in the array it will call a static method (using '$this' as the class name), and if offset 0 is a instance of a object, the signal will do the standard call $this->function();

Please look at the php manual, and realize that php-gtk is just a php extension and it does not modify php language itself

http://www.php.net/manual/en/language.oop5.basic.php
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

To make Orlando's point

To make Orlando's point slightly more visual.

<?php
class bobObject {
    public function
__construct() {
       
var_dump(
           
$this,
           
'$this',
           
"$this",
           
get_class($this)
        );
        return;
    }
   
    public function
__toString() {
        return
"This is a string of the object (so to say)";
    }
}

new
bobObject;
?>

jaina:~ bob$ php this.php

object(bobObject)#1 (0) {
}
string(5) "$this"
string(42) "This is a string of the object (so to say)"
string(9) "bobObject"

Only one surprise

I think that there is only one mildly surprising thing in this, and that is the case of "$this" vs $this, which bob's code shows well.

The reason why these don't show the same result as could be expected it happens is that

  • without the double quotes, var_dump actually receives $this to display,
  • with the double quotes, $this is first evaluated to a string, triggering the __toString "magic" method (See http://php.net/__toString), and only then is the resulting string passed to var_dump.

Bob's example, extended:

<?php
class bobObject
 
{
  public function
__construct()
    {
   
var_dump(array
      (
     
'plain'         => $this,
     
'quoted'        => '$this',
     
'double-quoted' => "$this",
     
'cast'          => (string) $this,
     
'class name'    => get_class($this),
      ));
    return;
    }
   
  public function
__toString()
    {
    return
"String representation of the object";
    }
  }

$o = new bobObject;
?>

Opções de exibição de comentários

Escolha seu modo de exibição preferido e clique em "Salvar configurações" para ativar.