kQuery()->"a faster, smarter active record ;-)"

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

echo kq('datatype #id')->each('strtoupper',array($this->name))->toXML();
fixed :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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');
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Gah? I'm not sure that will even work. Don't use __construct for that. Have a separate init function.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

but it does work...!

What are your thoughts about an init() function?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I thought constructors were not supposed to be used to return anything?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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));
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
Last edited by Christopher on Mon Mar 12, 2007 1:28 am, edited 1 time in total.
(#10850)
Post Reply