piaolankeke / phpquery

Automatically exported from code.google.com/p/phpquery
0 stars 0 forks source link

Iteration to return OBJECT #191

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
It is a sugestion, not a reporting.
So, in iteration, it is not returned as a object, but as a string.
But it could be returned as object and at the same time be used as string in 
any part of the code only using a magic method (not here to discuss magic 
methods) called __toString() that work's like this:

class bla {
    public function __construct() {
        $this->text = 'blabla....';
    }
    public function set_bla_text($bla_text) {
        $this->text = $bla_text;
    }
    public function __toString() {
        echo $this->text;
    }   
}
$bla = new bla;
$bla->set_bla_text("not only bla anymore");
echo $bla;
$bla->set_bla_text("Now changing bla's text");
echo $bla;

It is a simple way for solving the iteration return.

//////////////////////
And in additional there is other method called __invoke that is very nice :D
Lets say bla again:
class bla {
    public function __construct() {
        $this->text = 'blabla....';
    }
    public function set_bla_text($bla_text) {
        $this->text = $bla_text;
    }
    public function __toString() {
        echo $this->text;
    }
    public function __invoke($arg) {
        $this->set_bla_text($arg);
    }
}
$bla = new bla;
$bla->set_bla_text("not only bla anymore");
echo $bla;
$bla->set_bla_text("Now changing bla's text");
echo $bla;

$bla("New text off bla!!!!!");
echo $bla;

Look how interesting it became when using both PHP features..
OK that is it.

Guilherme Ferreira - Web developer - Brasil!!!

Original issue reported on code.google.com by guiferro...@gmail.com on 23 Aug 2011 at 7:18

GoogleCodeExporter commented 8 years ago
Look.. I read phpquery code and saw you know that all, and also that it is not 
possible cause it uses DomDocument and it does not have a __call method or 
something, but any way... How about thinking of extend the DOMDocument class in 
DOMDocumentWrapper ?
Well, i have not much time to expend but if i could help i would be glad..

;)

Original comment by guiferro...@gmail.com on 23 Aug 2011 at 7:57