stdClass

php.internals

Jakes

22 years ago
Please can some explain to me what the stdClass is used for in PHP 5. I've tried to use the reflection API to reverse engineer it but it does not give you much on the stdClass What methods can be inherited from this class? Does it work the same was as the java base class - Object?

Stig S. Bakken

22 years ago
On Fri, 2004-02-27 at 14:15, Jakes wrote:
> Please can some explain to me what the stdClass is used for in > PHP 5. I've tried to use the reflection API to reverse engineer it > but it does not give you much on the stdClass > > What methods can be inherited from this class? > > Does it work the same was as the java base class - Object?
No. There is no such thing as Java's base class built into PHP. stdClass is just an empty "dummy class" that is used internally, for example if you cast an array to an object. You can also use stdClass if you need an object of no particular class. - Stig

Stephan Schmidt

22 years ago
Hi,
> > Please can some explain to me what the stdClass is used for in > > PHP 5. I've tried to use the reflection API to reverse engineer it > > but it does not give you much on the stdClass > No. There is no such thing as Java's base class built into PHP. > stdClass is just an empty "dummy class" that is used internally, for > example if you cast an array to an object. You can also use > stdClass if > you need an object of no particular class.
Just one thing to add: It has been also available in PHP4. Stephan

Jakes Potgieter

22 years ago
It would be nice to have a base class to inherit some special methods as we do in Java. The same as method overloading, one of the most important aspects missing in PHP5 and the new object model. Thanks. ----- Original Message ----- From: "Stephan Schmidt" <schst@php-tools.net> Newsgroups: php.internals To: "'Jakes'" <sunssoffun@hotmail.com> Cc: <internals@lists.php.net> Sent: Friday, February 27, 2004 3:46 PM Subject: RE: [PHP-DEV] stdClass

Michael Walter

22 years ago
Jakes Potgieter wrote:
> It would be nice to have a base class to inherit some special > methods as we do in Java. [...]
What would be useful methods contained in the base class?

Hartmut Holzgraefe

22 years ago
Jakes Potgieter wrote:
> It would be nice to have a base class to inherit some special > methods as we do in Java.
could you name which ones you think of? being out of the JAVA camp for several years know i no longer know all java.object methods and properties by heart, but PHP mechanisms like __sleep()/__wakeup(), __toString() and the introspection related functions like get_class() and friends should provide comparable behaviour ... (IMHO the fact that any object in JAVA can be casted down to java.object is not that clever after all, and part of it is only due to the fact that there is no other way to have portable container classes, which is now finally solved with generics)
> The same as method overloading, > one of the most important aspects missing in PHP5 and the > new object model.
due to the feature of optional parameters you may end up with more than one overloaded function matching a call signature, due to the fact that argument types in a function call are not known at compile time signature matching would have to happen at runtime, and the language syntax would need to be extended to add parameter type specifiers to function parameter declarations (ok, we already have this one for interfaces ...)
-- Hartmut Holzgraefe <hartmut@php.net>

Jakes Potgieter

22 years ago
When Im talking about something like this would come in handy obviously doesnt work! The paramter null could be added, to get desired result but to be programmatically correct: class MyOverload{ function __construct($Foo) { $this->Foo = $Foo; } // overload the constructor function __construct(String $Foo, String $Bar){ $this->Foo = $Foo; $this->Bar = $Bar; } public function doSomething(){ if($this->Foo != null && $this->Bar == null){ return $this->Foo . " needs a partner"; // do something with Foo }elseif($this->Foo != null && $this->Bar != null){ return $this->Foo . " and " . $this->Bar . " are partners"; //do something with Foo and Bar } } private $Foo; private $Bar; } $obj = new MyOverload("Mary"); print $obj->doSomething(); ----- Original Message ----- From: "Hartmut Holzgraefe" <hartmut@php.net> To: "Jakes Potgieter" <jakespotgieter@hotmail.com> Cc: "Stephan Schmidt" <schst@php-tools.net>; <internals@lists.php.net> Sent: Friday, February 27, 2004 4:20 PM Subject: Re: [PHP-DEV] stdClass

Jakes

22 years ago
Oh, yes and special methods that you mentioned would come in handy too but from a object perspective. Maybe explicitly calling some sort finalize() method to clean un-referenced objects would also be handy. ----- Original Message ----- From: "Jakes Potgieter" <jakespotgieter@hotmail.com> Newsgroups: php.internals To: <hartmut@php-groupies.de> Cc: <internals@lists.php.net>; "Stephan Schmidt" <schst@php-tools.net> Sent: Friday, February 27, 2004 4:42 PM Subject: Re: [PHP-DEV] stdClass
> When Im talking about something like this would come in handy > obviously doesnt work! The paramter null could be added, to get > desired result but to be programmatically correct: > > class MyOverload{ > function __construct($Foo) { > $this->Foo = $Foo; > } > // overload the constructor > function __construct(String $Foo, String $Bar){ > $this->Foo = $Foo; > $this->Bar = $Bar; > } > public function doSomething(){ > if($this->Foo != null && $this->Bar == null){ > return $this->Foo . " needs a partner"; > // do something with Foo > }elseif($this->Foo != null && $this->Bar != null){ > return $this->Foo . " and " . $this->Bar . " are partners";
//do

Michael Walter

22 years ago
Jakes wrote:
> Oh, yes and special methods that you mentioned would come in handy too > but from a object perspective. Maybe explicitly calling some sort finalize() > method to clean un-referenced objects would also be handy.
How does that differ from $x=null; with a reference-counting garbage collector? Cheers, Michael

Jakes

22 years ago
You calling it from the base so cleaning you be easier than looking for object referenced variables and setting them to null. Working in a large class hierarchy it could get painful. "Michael Walter" <cm@leetspeak.org> wrote in message news:403F5A6B.3000405@leetspeak.org...
> Jakes wrote: > > Oh, yes and special methods that you mentioned would come in handy too > > but from a object perspective. Maybe explicitly calling some sort
finalize()

Hartmut Holzgraefe

22 years ago
Jakes wrote:
> You calling it from the base so cleaning you be easier than > looking for object referenced variables and setting them to null. > Working in a large class hierarchy it could get painful.
__destruct() and as far as object references in variables are concerned: the engine destructs all instance member variables when an object is destructed, so the reference counts of the referenced objects are already deceremented no need to free them by hand (this is even true for PHP 4) you only have to take care of cleanup yourself when you have circular refence graphs ...
-- Hartmut Holzgraefe <hartmut@php.net>

Hartmut Holzgraefe

22 years ago
Jakes wrote:
> Oh, yes and special methods that you mentioned would come in handy too > but from a object perspective. Maybe explicitly calling some sort finalize() > method to clean un-referenced objects would also be handy.
finalize() is one of the worst concepts of JAVA that i can think of: a somehow-destructor called at a non-forseeable time in the future (whenever the garbage collector thread tinks its about time to reclaim the only resource it is able to monitor: memory) there are even recommedations not to use it at all ...
-- Hartmut Holzgraefe <hartmut@php.net>

Michael Walter

22 years ago
Hartmut Holzgraefe wrote:
> Jakes wrote: > >> Oh, yes and special methods that you mentioned would come in handy too >> but from a object perspective. Maybe explicitly calling some sort >> finalize() >> method to clean un-referenced objects would also be handy. > > > finalize() is one of the worst concepts of JAVA that i can think of: > a somehow-destructor called at a non-forseeable time in the future > (whenever the garbage collector thread tinks its about time to reclaim > the only resource it is able to monitor: memory) > > there are even recommedations not to use it at all ...
Indeed. UNWIND-PROTECT a.k.a. try { .. } finally { .. } in Java or using () { .. } in C# are usually a much better choice, I think. Similar problems apply to PHP __destructors, too, anyway (mainly the non-derminism of cleanup), probably those should be used with similar care ;-) Cheers, Michael