OOP in PHP5
-
Alexey Zakhlestine
OOP in PHP5
Wed June 24 2009 11:35:07 UTCNot directly midgard-related, but still should be interesting to
people who work with PHP5 and objects (midcom-developers)
I saw previous edition of this talk in 2008. I think it's one of the
best presentations on this subject. Marcus did a great job
http://somabo.de/talks/200903_montreal_oop.pdf
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Thu June 25 2009 07:50:07 UTCAlexey Zakhlestin wrote:
> Not directly midgard-related, but still should be interesting to
> people who work with PHP5 and objects (midcom-developers)
>
> I saw previous edition of this talk in 2008. I think it's one of the
> best presentations on this subject. Marcus did a great job
>
> http://somabo.de/talks/200903_montreal_oop.pdf
>
Indeed very good.
However some sort of "truth table" of implicit vs explicit references on
objects, their properties and other types would be very good to have (as
noted in the other discussion), something along the lines of:
function get_singleton()
{
static $singleton = null
if (!$singleton)
{
$singleton = new myobject();
}
return $singleton;
}
$object = get_singleton(); // Implicit reference
$property = $object->property; // COW ? (or implicit reference ?)
$string = 'foo';
$string_too = $string; // COW
/Rambo
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Fri June 26 2009 08:10:06 UTCOn Thu, Jun 25, 2009 at 11:31 AM, Eero af
Heurlin<eero.afheurlin@nemein.com> wrote:
> However some sort of "truth table" of implicit vs explicit references on
> objects, their properties and other types would be very good to have (as
> noted in the other discussion), something along the lines of:
>
> function get_singleton()
> {
> static $singleton = null
> if (!$singleton)
> {
> $singleton = new myobject();
> }
> return $singleton;
> }
>
> $object = get_singleton(); // Implicit reference
> $property = $object->property; // COW ? (or implicit reference ?)
depends on what "property" is.
1) if it is "usual" var — copy-on-write of var
2) if it is "pointer to object" — copy-on-write of "pointer to object"
3) "$property = &$object->property;" — reference to whatever original
property was
I said "pointer to object" and not "object" because that is the only
thing there is in php5. php4 had objects, php5 only has "pointers to
objects" (but we still call them objects)
cases (1) and (2) won't need additional memory at the moment of assignment
case (3) will require a bit of memory-allocation immediately
> $string = 'foo';
> $string_too = $string; // COW
>
> /Rambo
> _______________________________________________
> dev mailing list
> dev@lists.midgard-project.org
> http://lists.midgard-project.org/mailman/listinfo/dev
>
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Fri June 26 2009 10:15:07 UTCAlexey Zakhlestin wrote:
>> function get_singleton()
>> {
>> static $singleton = null
>> if (!$singleton)
>> {
>> $singleton = new myobject();
>> }
>> return $singleton;
>> }
>>
>> $object = get_singleton(); // Implicit reference
>> $property = $object->property; // COW ? (or implicit reference ?)
>
> depends on what "property" is.
> 1) if it is "usual" var — copy-on-write of var
> 2) if it is "pointer to object" — copy-on-write of "pointer to object"
So if we have
class object
{
var $property = 'foo'
}
-> it's case 1
class object
{
var $property = null
function __construct()
{
$this->property = new another_object
}
}
-> case 2, but what happens in the following
$property->another_property = 'foo';
ie, since $property is "pointer to object" how does the COW here work ?
Another interesting case
class object
{
var $property = null
function __construct()
{
$this->property = another_object::get_singleton(); // Returns a
refrence
}
}
now what happens with $property = $object->property? Do we have a COW of
the reference meaning:
$property = 'foo';
Will do the COW and $object->property stays as the singleton, or do we
overwrite the singleton ?
(Yes, I do have a destructive mindset [at times]... Why do you think I
got the license to legally handle explosives... :)
This get's especially interesting with the midgard_objects that
AFAIUndestand are actually proxies/references to the GObjects these days
instead of "normal" PHP objects.
Another question:
$foo = 'foo';
$foo2 = $foo;
$foo = 'bar';
What is the value of $foo2 ? As I would understand it should be 'foo'.
My point is that at times (though this may be a side-effect the the
ragnaroek decorators too) with the following pseudocode
function do_something(&$object)
{
$prop_orig_value = $object->property;
$object->property = 'new value';
}
$prop_orig_value gets value 'new value' as well, I need to use
$prop_orig_value = (string)$object->property;
to make an explicit copy (in this case I knew it must be a string)
/rambo
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Fri June 26 2009 10:35:05 UTCOn Fri, Jun 26, 2009 at 2:03 PM, Eero af
Heurlin<eero.afheurlin@nemein.com> wrote:
> Alexey Zakhlestin wrote:
>> depends on what "property" is.
>> 1) if it is "usual" var — copy-on-write of var
>> 2) if it is "pointer to object" — copy-on-write of "pointer to object"
>
>
> So if we have
>
> class object
> {
> var $property = 'foo'
> }
>
> -> it's case 1
yes.
> class object
> {
> var $property = null
> function __construct()
> {
> $this->property = new another_object
> }
> }
>
> -> case 2, but what happens in the following
>
> $property->another_property = 'foo';
>
> ie, since $property is "pointer to object" how does the COW here work ?
if we are talking about this:
$obj = new object();
$property = $obj->property;
$property->another_property = 'foo';
then $obj->property and $property point to the same object and
$property->another_property will change value of "another_property" in
both of them
but important bit is, that saying "$property = 'bar'" will change only
the value of $property not of $obj->property (because they are not
"references", but separate variables pointing to the same object) ;)
> Another interesting case
>
> class object
> {
> var $property = null
> function __construct()
> {
> $this->property = another_object::get_singleton(); // Returns a
> refrence
> }
> }
>
> now what happens with $property = $object->property? Do we have a COW of
> the reference meaning:
>
> $property = 'foo';
>
> Will do the COW and $object->property stays as the singleton, or do we
> overwrite the singleton ?
that would still be copy-on-write. overwriting $this->property will
overwrite original singleton-holder, but overwriting $property won't
influence them.
> (Yes, I do have a destructive mindset [at times]... Why do you think I
> got the license to legally handle explosives... :)
>
> This get's especially interesting with the midgard_objects that
> AFAIUndestand are actually proxies/references to the GObjects these days
> instead of "normal" PHP objects.
>
> Another question:
>
> $foo = 'foo';
> $foo2 = $foo;
> $foo = 'bar';
>
> What is the value of $foo2 ? As I would understand it should be 'foo'.
yes. it is 'foo'
> My point is that at times (though this may be a side-effect the the
> ragnaroek decorators too) with the following pseudocode
>
>
> function do_something(&$object)
> {
> $prop_orig_value = $object->property;
> $object->property = 'new value';
> }
>
> $prop_orig_value gets value 'new value' as well, I need to use
>
> $prop_orig_value = (string)$object->property;
>
> to make an explicit copy (in this case I knew it must be a string)
if there is such behaviour, it means, that "variable separation"
procedure wasn't implemented and it is a bug in midgard
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Fri June 26 2009 19:45:06 UTCEero af Heurlin writes:
Hi!
> This get's especially interesting with the midgard_objects that
> AFAIUndestand are actually proxies/references to the GObjects these days
> instead of "normal" PHP objects.
Object is one thing and property write/read is another.
Any MgdSchema (and any Midgard) object in PHP is usual PHP object.
It "only" holds a pointer to underlying GObject. So for example:
$obj = new org_midgardproject_example();
unset($obj)
Will invoke custom PHP routines to destroy object and additionally
underlying GObject constructor. Well... almost, as underlying GObject
will be destroyed *correctly* while PHP not necessarily.
Best example is well know memory abuse "bug":
foreach ($i = 0 ; $i < 10000; $i++)
{
$obj = new midgard_mgdschema_object();
}
But this case is just not mentioned in my "ZE2 for Dummies" book ;)
Properties are another thing:
$obj = new midgard_mgdschema_object();
$obj->property = 'foo';
$obj->property = $some_var;
$obj->property = &$some_reference;
In every case, nothing is done with original value, and it really
doesn't matter what kind of value we have.
We just set underlying property. Even if it's reference, we do not make
shallow copy like ZE2 engine does.
(But this is OT as it's ZE2 thing and not PHP).
Read property handler is different:
$val = $obj->property will *always* create new variable.
This should be improved.
Piotras
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Sat June 27 2009 10:35:06 UTCOn Fri, Jun 26, 2009 at 11:36 PM, Piotr Pokora<piotrek.pokora@gmail.com> wrote:
> Best example is well know memory abuse "bug":
>
> foreach ($i = 0 ; $i < 10000; $i++)
> {
> $obj = new midgard_mgdschema_object();
> }
>
> But this case is just not mentioned in my "ZE2 for Dummies" book ;)
actually, memory usage will grow only until garbage-collector starts.
I don't remember details of when this happens, but we can do some
tests
> Properties are another thing:
>
> $obj = new midgard_mgdschema_object();
>
> $obj->property = 'foo';
> $obj->property = $some_var;
> $obj->property = &$some_reference;
>
> In every case, nothing is done with original value, and it really
> doesn't matter what kind of value we have.
> We just set underlying property. Even if it's reference, we do not make
> shallow copy like ZE2 engine does.
> (But this is OT as it's ZE2 thing and not PHP).
that's still an interesting topic to talk about. I think, that we
actually should follow normal zend-objects patterns.
> Read property handler is different:
>
> $val = $obj->property will *always* create new variable.
> This should be improved.
Well, as long as we have actual zvals for all properties this should
be trivial. We should move in this direction.
--
Alexey Zakhlestin
http://www.milkfarmsoft.com/
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev -
Re: [midgard-dev] OOP in PHP5
Sat June 27 2009 19:50:05 UTCAlexey Zakhlestin writes:
Hi!
> On Fri, Jun 26, 2009 at 11:36 PM, Piotr Pokora<piotrek.pokora@gmail.com> wrote:
>
>> Best example is well know memory abuse "bug":
>>
>> foreach ($i = 0 ; $i < 10000; $i++)
>> {
>> $obj = new midgard_mgdschema_object();
>> }
>>
>> But this case is just not mentioned in my "ZE2 for Dummies" book ;)
>
> actually, memory usage will grow only until garbage-collector starts.
No. Problem is midgard_metadata property which is not automagically
destroyed.
And I can not find any code which could help here.
Besides ZE2 is anything but user friendly in such case and feeds you
with random memory.
Just try memory_get_usage() and look at php_midgard_gobject_dtor
disabled code.
>> Properties are another thing:
>>
>> $obj = new midgard_mgdschema_object();
>>
>> $obj->property = 'foo';
>> $obj->property = $some_var;
>> $obj->property = &$some_reference;
>>
>> In every case, nothing is done with original value, and it really
>> doesn't matter what kind of value we have.
>> We just set underlying property. Even if it's reference, we do not make
>> shallow copy like ZE2 engine does.
>> (But this is OT as it's ZE2 thing and not PHP).
>
> that's still an interesting topic to talk about. I think, that we
> actually should follow normal zend-objects patterns.
Yes, that's why we use standard property write handler here.
So we set GObject property and let ZE2 do what it should to do.
> Well, as long as we have actual zvals for all properties this should
> be trivial. We should move in this direction.
Yes, but we need get_property_ptr_ptr handler, which, as you noticed
seems to be buggy.
Another thing is where property zval comes from.
Registered as class' default property would be perfect but that didn't
seem to work.
Piotras
_______________________________________________
dev mailing list
dev@lists.midgard-project.org
http://lists.midgard-project.org/mailman/listinfo/dev
