Welcome Guest, Not a member yet? Register   Sign In
Objects: Why are they useful?
#19

[eluser]tonanbarbarian[/eluser]
[quote author="wiredesignz" date="1201064762"]
PHP4 Objects can have properties and functions added programtically also. Therefore PHP4 can extend a $query_row() object.[/quote]

Yes that is true
However the $query->result_object() in CI does not let you pass the initial object to be extended.
If you wanted the result_object to return an array of a particular class that was created and you just wanted it to set the properties of that object it would not be able to do it.

Having said that, in PHP 4 at least (not sure about PHP5) it is slow to programatically add properties to an existing object. I can only assume adding methods is slow as well.

for example if you have an existing class definition like this
Code:
class Something {
  var $foo = null;
} // class Something
then we instantiate it
Code:
$obj = new Something();
Assigning a value to the property fo is fine because it is already declared
Code:
$obj->foo = 123;
But if we assign a value to a property that does not yet exist this is relatively slow
Code:
$obj->bar = 456;
I believe this is because PHP has to redeclare the object in memory to add this property

As Nick says this is a great feature that allows a lot of flexibility that you just cannot do in some other OO languages, however if you want everything to run fast avoid creating new properties on objects if you can help it

That also means avoid using stdClass, because then every property you add to it has to be redeclared.
In fact if you are considering using stdClass you should think about using an array instead.

stdClass, for those that do not know, is a way of create a new object as an empty class
Code:
$obj = new stdClass();
$obj->foo=123;
$obj->bar=456;

Not 100% sure but the trick here, if you really need to create an empty class and then assign properties to it, this method might be quicker
Code:
$obj = array();
$obj['foo']=123;
$obj['bar']=456;
$obj = (object)$obj;

So the idea here is to create an array first and assign items to to, because array processing is quick
then cast it to an object and you have an object with the properties you want and hopefully it has done it quicker than the previous method. (One day when I get some time I might check if this is really quicker or not)


Messages In This Thread
Objects: Why are they useful? - by El Forum - 01-22-2008, 02:59 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:18 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 03:32 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:02 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:20 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:22 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:31 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:38 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:41 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:49 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:51 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 04:52 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:01 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:06 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 05:19 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 06:55 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:15 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 07:23 PM
Objects: Why are they useful? - by El Forum - 01-22-2008, 08:15 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:47 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 05:59 PM
Objects: Why are they useful? - by El Forum - 01-24-2008, 06:34 PM
Objects: Why are they useful? - by El Forum - 01-25-2008, 02:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB