Page 2 of 4

Posted: Sun Mar 11, 2007 1:56 pm
by Ollie Saunders

Code: Select all

echo kq('datatype #id')->each('strtoupper',array($this->name))->toXML();
fixed :)

Posted: Sun Mar 11, 2007 2:17 pm
by Kieran Huggins
Show and tell sandbox time (comments welcome):

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');

Posted: Sun Mar 11, 2007 2:21 pm
by Ollie Saunders
Gah? I'm not sure that will even work. Don't use __construct for that. Have a separate init function.

Posted: Sun Mar 11, 2007 2:29 pm
by Kieran Huggins
but it does work...!

What are your thoughts about an init() function?

Posted: Sun Mar 11, 2007 2:47 pm
by RobertGonzalez
I thought constructors were not supposed to be used to return anything?

Posted: Sun Mar 11, 2007 3:02 pm
by Ollie Saunders
Everah, yeah so did I. Possibly once instantiated you can call the constructor again and get return values.

Several minutes later....yep looks like you can

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);
I'd definitely say its a bad idea though, readability-wise. Constructors are for creating instances not for whatever funky stuff you are trying to do there kieran.

Posted: Sun Mar 11, 2007 3:07 pm
by Kieran Huggins
I'm just returning a reference to the object - is that taboo? I have no idea why it would be :-(

What could go wrong? (honest question, not rhetorical)

Posted: Sun Mar 11, 2007 3:20 pm
by Ollie Saunders
Well you just seem to be making it harder than it need be.

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));

Posted: Sun Mar 11, 2007 6:40 pm
by Kieran Huggins
@ole: not exactly the syntax I was hoping for - I think a big part of the appeal to me is the "poetic" nature of chained selects.

Can anyone tell me why returning from __construct() is bad form? I can't find much on the net (though I probably just don't know what to search for..)

I saw a semi-related bug or two in php 4.xx, but nothing in 5.x

Posted: Sun Mar 11, 2007 7:05 pm
by jayshields
AFAIK constructors should only be used for initializing an object. You should never need to return anything while initiating an object.

There's probably more to it than that, although that's all I know.

Posted: Sun Mar 11, 2007 8:11 pm
by Begby
Constructors are supposed to return a reference to the newly created object and thats it. I am suprised it doesn't explode when you try to do it. I would definitely not code something to rely on returning something special from a constructor as that is liable to break at some point.

Posted: Sun Mar 11, 2007 9:03 pm
by RobertGonzalez
Ok, here's my thought...

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;
?>
Is $test_class an object of the Test_This class, or is it the string value assigned to $test_class upon instantiation? It just seems like a conflict to me, even if it is not really a conflict. You know what I mean?

Posted: Sun Mar 11, 2007 9:59 pm
by Kieran Huggins
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?

Posted: Sun Mar 11, 2007 10:09 pm
by feyd
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.

Posted: Sun Mar 11, 2007 10:33 pm
by Christopher
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.
Oops meant to quote Kieran:
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?
But your constructor is not really returning anything -- new is.