Re: [PHP] class constructor overloading

2008-10-23 Thread Christoph Boget
>> is it possible to overload the class construct(or) ?
>> if yes, how ?

No, it's not.

> class A
> {
>function __construct()
>{
>echo "A";
>}
> }
>
> class B extends A
> {
>function __construct()
>{
>echo "B";
>parent::__construct();
>}
> }
> $B = new B();

The above is an example of overriding, not overloading; PHP allows the
former but not the latter.

Examples of overloading include:

class A
{
  function myFunc( $boolean )
  {

  }

  function myFunc( $string )
  {

  }

  function myFunc( $array )
  {

  }
}

Because PHP is loosely typed, the above is largely moot/unnecessary.

class B
{
  function myFunc( $boolean )
  {

  }

  function myFunc( $boolean, $string )
  {

  }

  function myFunc( $boolean, $string, $array )
  {

  }
}

PHP allows you to get around the above by allowing you to define
default values for arguments.  Doing that is kind of like overloading
but only in a fake-me-out kind of way.  So instead of overloading the
myFunc method, you could just define it as follows:

class B
{
  function myFunc( $boolean = FALSE, $string = '', $array = array())
  {

  }
}

but even that doesn't ensure that the data type of each argument is as
you might expect.  As I stated above, PHP is loosely typed so there is
nothing preventing, say, the $string argument from actually being a
numeric datatype.  If you are using PHP5+, you can sort of get around
that by using type hinting
(http://us.php.net/manual/en/language.oop5.typehinting.php) but you
can only type hint objects and arrays.

In closing, to answer your question,

Overriding: yes
Overloading: no

thnx,
Chris

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



Re: [PHP] class constructor overloading

2008-10-23 Thread Jochem Maas
Alain Roger schreef:
> thanks a lot, this is exactly what i needed.
> if the construct of based class A accept arguments, i guess that construct
> of class B must have the sames.
> moreover, i guess that something like that must be written:

I guess you find guessing preferable to RTFM and/or trying code out.

> class A
> {
>   function __construct($nameA)
>   {
> ...
>   }
> }
> 
> class B extends A
> {
>   function __construct($nameB)
>   {
> parent::__construct($nameB);
>   }
> }
> 
> am i right ?

are you?

> 
> thanks.
> 
> A.
> 


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



Re: [PHP] class constructor overloading

2008-10-23 Thread David Otton
2008/10/23 Alain Roger <[EMAIL PROTECTED]>:
> thanks a lot, this is exactly what i needed.
> if the construct of based class A accept arguments, i guess that construct
> of class B must have the sames.

No, you can change the signature of a method when you overload it.
Below, B::__construct() accepts 1 argument while A::__construct()
accepts 0 arguments:

class A
{
   function __construct()
   {
   echo "A";
   }
}

class B extends A
{
   function __construct( $string )
   {
   echo $string;
   parent::__construct();
   }
}

$B = new B( "B" );

If you need to force a certain signature in child classes, you can
declare the parent class abstract:

abstract class A
{
   abstract function f( $a );
}

class B extends A
{
function f( $a, $b ) // throws an error
{
echo "$a, $b";
}
}

$B = new B("B", "C");

However, if you declare the constructor as abstract, it will be ignored.

-- 

http://www.otton.org/

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



Re: [PHP] class constructor overloading

2008-10-23 Thread Jim Lucas

Alain Roger wrote:

thanks a lot, this is exactly what i needed.
if the construct of based class A accept arguments, i guess that construct
of class B must have the sames.
moreover, i guess that something like that must be written:
class A
{
  function __construct($nameA)
  {
...
  }
}

class B extends A
{
  function __construct($nameB)
  {
parent::__construct($nameB);
  }
}

am i right ?

thanks.

A.



Yes


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



Re: [PHP] class constructor overloading

2008-10-23 Thread Alain Roger
thanks a lot, this is exactly what i needed.
if the construct of based class A accept arguments, i guess that construct
of class B must have the sames.
moreover, i guess that something like that must be written:
class A
{
  function __construct($nameA)
  {
...
  }
}

class B extends A
{
  function __construct($nameB)
  {
parent::__construct($nameB);
  }
}

am i right ?

thanks.

A.


Re: [PHP] class constructor overloading

2008-10-23 Thread David Otton
2008/10/23 Alain Roger <[EMAIL PROTECTED]>:

> is it possible to overload the class construct(or) ?
> if yes, how ?

class A
{
function __construct()
{
echo "A";
}
}

class B extends A
{
function __construct()
{
echo "B";
parent::__construct();
}
}

$B = new B();

-- 

http://www.otton.org/

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



Re: [PHP] Class constructor

2006-02-01 Thread Marcus Bointon


On 31 Jan 2006, at 13:52, David Grant wrote:


Drop __construct, PHP5 will call Test() anyway.

From http://uk.php.net/manual/en/language.oop5.decon.php:

"For backwards compatibility, if PHP 5 cannot find a __construct()
function for a given class, it will search for the old-style  
constructor

function, by the name of the class."


Sure, if you're planning on writing 'backwards' code for ever more...  
For maximum efficiency in PHP5, do it this way around:


class test{
function Test(){
$this->__construct()
//This will be called in PHP4
}
function __construct(){
//This will be called in PHP5
}
}

Why penalise the platform you're intending to run it on?

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
[EMAIL PROTECTED] | http://www.synchromedia.co.uk

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



Re: [PHP] Class constructor

2006-01-31 Thread Richard Lynch
On Tue, January 31, 2006 7:46 am, Georgi Ivanov wrote:
> I'm writing a simple class. In order to be compatible with php4 and
> php5 I've
> done this :
>
> class test{
>   function Test(){
>   //This will be called in PHP4
>   }
>   function __construct(){
>   //This will be called in PHP5
>   $this->Test();
>   }
> }
>
> Is this a solution ? Is there a better way ?

I suppose if you want the code to run under PHP6, 7, 8, 9, 10, ... it
is somewhat forward-looking to the potential day when Test::Test()
might no longer work as the constructor...

-- 
Like Music?
http://l-i-e.com/artists.htm

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



Re: [PHP] Class constructor

2006-01-31 Thread Georgi Ivanov
Yes, I noticed that PHP5 call the old way constructor, but what if in future 
version they drop this feature ?
I think this is workaround for the problem . Or not ?

On Tuesday January 31 2006 15:52, David Grant wrote:
> Georgi,
>
> Drop __construct, PHP5 will call Test() anyway.
>
> >From http://uk.php.net/manual/en/language.oop5.decon.php:
>
> "For backwards compatibility, if PHP 5 cannot find a __construct()
> function for a given class, it will search for the old-style constructor
> function, by the name of the class."
>
> David
> --
> David Grant
> http://www.grant.org.uk/
>
> http://pear.php.net/package/File_Ogg0.2.1
> http://pear.php.net/package/File_XSPF   0.1.0
>
> WANTED: Junior PHP Developer in Bristol, UK

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



Re: [PHP] Class constructor

2006-01-31 Thread David Grant
Georgi,

Drop __construct, PHP5 will call Test() anyway.

>From http://uk.php.net/manual/en/language.oop5.decon.php:

"For backwards compatibility, if PHP 5 cannot find a __construct()
function for a given class, it will search for the old-style constructor
function, by the name of the class."

David
-- 
David Grant
http://www.grant.org.uk/

http://pear.php.net/package/File_Ogg0.2.1
http://pear.php.net/package/File_XSPF   0.1.0

WANTED: Junior PHP Developer in Bristol, UK

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



Re: [PHP] Class constructor

2006-01-31 Thread Andrei

You could try with phpversion().

Georgi Ivanov wrote:

Hi,
I'm writing a simple class. In order to be compatible with php4 and php5 I've 
done this :


class test{
function Test(){
//This will be called in PHP4
}
function __construct(){
//This will be called in PHP5
$this->Test();
}
}

Is this a solution ? Is there a better way ?

Thanks.



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



Re: [PHP] class constructor & polymorphism

2005-04-08 Thread Josip Dzolonga
[EMAIL PROTECTED] wrote:

Hi to all,
I'm a C++ programmer and I've to convert some simple classes
from C++ to PHP. My toolbar_button class must have two or more
constructors so I ask you if this is possible with PHP:
You can't overload a constructor, I mean a function in PHP.  Maybe 
extending two classes from the base would be a good work-around.

Hope this helps,
--
Josip Dzolonga
http://josip.dotgeek.org
jdzolonga[at]gmail.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php