OOP: __construct() and "extend"

OOP: __construct() and "extend"

am 22.12.2009 23:45:49 von APseudoUtopia

Hey list,

I'm writing my own class which extends upon the Memcached class
(php.net/memcached).

I'm a bit confused as to how the constructor works when extending a class.

class caching extends Memcached {
function __construct() {
echo "Caching Class Construct!";
}
}

For something like the above code, is the Memcached constructor
called? Or do I have to explicitly call it via parent::__construct()
within the caching class?

Thanks.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: OOP: __construct() and "extend"

am 22.12.2009 23:53:24 von Jonathan Tapicer

Hey,

Constructors behave the same way as regular methods with inheritance,
you have to manually call the parent constructor, ie:
parent::__construct().

Regards,

Jonathan

On Tue, Dec 22, 2009 at 7:45 PM, APseudoUtopia wrote:
> Hey list,
>
> I'm writing my own class which extends upon the Memcached class
> (php.net/memcached).
>
> I'm a bit confused as to how the constructor works when extending a class.
>
> class caching extends Memcached {
> function __construct() {
> echo "Caching Class Construct!";
> }
> }
>
> For something like the above code, is the Memcached constructor
> called? Or do I have to explicitly call it via parent::__construct()
> within the caching class?
>
> Thanks.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: OOP: __construct() and "extend"

am 23.12.2009 07:11:23 von Allen McCabe

--00504502bd71235ebd047b5f3186
Content-Type: text/plain; charset=ISO-8859-1

Extended classes may have their own constructors which are implicitly
called, and as Jonathan mentioned, the constructor of any ancestors (ie. the
parent) must explicitly be called.

If the child (extended) class does NOT have it's own constructor method
defined, the parent's constructor is called.

Manual: http://php.net/manual/en/language.oop5.decon.php


On Tue, Dec 22, 2009 at 2:45 PM, APseudoUtopia wrote:

> Hey list,
>
> I'm writing my own class which extends upon the Memcached class
> (php.net/memcached).
>
> I'm a bit confused as to how the constructor works when extending a class.
>
> class caching extends Memcached {
> function __construct() {
> echo "Caching Class Construct!";
> }
> }
>
> For something like the above code, is the Memcached constructor
> called? Or do I have to explicitly call it via parent::__construct()
> within the caching class?
>
> Thanks.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--00504502bd71235ebd047b5f3186--