Posted: Sun Mar 11, 2007 1:56 pm
Code: Select all
echo kq('datatype #id')->each('strtoupper',array($this->name))->toXML();A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
echo kq('datatype #id')->each('strtoupper',array($this->name))->toXML();Code: Select all
class kQuery{
function __construct(){
// no arguments? return a blank kQuery object
if(func_num_args()==0) return $this;
// what arguments are there?
$a = func_get_args();
// testing the variable number of arguments
foreach($a as $arg) echo '<br/>'.$arg;
// return the kQuery object
return $this;
}
function test(){
$a=func_get_args();
echo '<br/>this is a '.implode(', ',$a).' kQuery object!';
return $this;
}
}
// assign the kQuery object maker
$k = create_function('','$args=func_get_args(); return call_user_func_array(array(new kQuery(),"__construct"),$args);');
// perform a (very) simple POC test:
$k('bob','frank')->test('super','krazy')->test('groovy','radical');Code: Select all
<?php
ini_set('display_errors', true);
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_WARNING, true);
class Foo
{
public function __construct($a)
{
$this->bar = $a;
return $a + 5;
}
}
$a = new Foo(5);
assert($a->bar == 5);
assert($a->__construct(4) == 9);
assert($a->bar == 4);Code: Select all
<?php
class kQuery
{
public function setSelector($params)
{
if (is_object($params)) { // array cast on objects is inappropriate
$params = array($params);
}
$this->selector = (array)$params;
}
public function getSelector()
{
return $this->selector;
}
}
function k()
{
$inst = new kQuery();
$args = func_get_args();
$inst->setSelector($args);
return $inst;
}
var_dump(k($input = 'foo')->getSelector() == array($input));Code: Select all
<?php
class Test_This
{
private $class_var;
public function __construct()
{
$this->class_var = 'I am now set!';
return $this->class_var;
}
}
$test_class = new Test_This;
?>Oops meant to quote Kieran:feyd wrote:The purpose of the constructor is to initialize the object only. Nothing more. They are not supposed to return anything because they are automatically called during the invocation of the "new" operator.
But your constructor is not really returning anything -- new is.Kieran Huggins wrote:I guess it doesn't make a difference in my case since I'm always returning the object itself, which is the purpose of the constructor anyway, isn't it?