php-general Digest 4 Jun 2013 08:58:15 -0000 Issue 8258

Topics (messages 321341 through 321344):

Re: Seemingly incorrect strict standard.
        321341 by: Matijn Woudt
        321342 by: Richard Quadling
        321343 by: shiplu
        321344 by: Richard Quadling

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Mon, Jun 3, 2013 at 7:24 PM, Richard Quadling <rquadl...@gmail.com>wrote:

> Hi.
>
> I've got an abstract class which requires one of the concrete descendants
> to implement a static function.
>
> The base class will call it using static:: rather than self::.
>
> But I'm getting an error at runtime.
>
> Static function should not be abstract.
>
> It doesn't SEEM right to inhibit this.
>
> Am I missing something?
>
> I'm on PHP 5.4.15 (Mac and Centos).
>
>
Hi Richard,

This change is done on purpose.
>From the PHP manual:
"Dropped abstract static class functions. Due to an oversight, PHP 5.0.x
and 5.1.x allowed abstract static functions in classes. As of PHP 5.2.x,
only interfaces can have them."

I believe you could make yourself in trouble when using it, and that's why
there is a warning with strict.

It does make sense, because overloading is something that works on classes,
and static functions do not reference classes.

- Matijn

--- End Message ---
--- Begin Message ---
Aha!

Don't make it abstract in my base class, but I can throw an exception
(perfectly reasonable for me as the called class should implement the
method).


On 3 June 2013 18:36, Matijn Woudt <tijn...@gmail.com> wrote:

>
> On Mon, Jun 3, 2013 at 7:24 PM, Richard Quadling <rquadl...@gmail.com>wrote:
>
>> Hi.
>>
>> I've got an abstract class which requires one of the concrete descendants
>> to implement a static function.
>>
>> The base class will call it using static:: rather than self::.
>>
>> But I'm getting an error at runtime.
>>
>> Static function should not be abstract.
>>
>> It doesn't SEEM right to inhibit this.
>>
>> Am I missing something?
>>
>> I'm on PHP 5.4.15 (Mac and Centos).
>>
>>
> Hi Richard,
>
> This change is done on purpose.
> From the PHP manual:
> "Dropped abstract static class functions. Due to an oversight, PHP 5.0.x
> and 5.1.x allowed abstract static functions in classes. As of PHP 5.2.x,
> only interfaces can have them."
>
> I believe you could make yourself in trouble when using it, and that's why
> there is a warning with strict.
>
> It does make sense, because overloading is something that works on
> classes, and static functions do not reference classes.
>
> - Matijn
>



-- 
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
Show a short reproducible code.

--- End Message ---
--- Begin Message ---
On 3 June 2013 20:37, shiplu <shiplu....@gmail.com> wrote:

> Show a short reproducible code.
>
Short-ish ...

<?php
abstract class baseClass {

    static public function generateLocalisedContent($s_Stuff){
        throw new Exception('Must implement static public function ' .
get_called_class() . '::generateLocalisedContent().');
    }

    public function __construct($s_Stuff){
        echo static::generateLocalisedContent($s_Stuff);
    }
}

class sub1 extends baseClass{}

class sub2 extends baseClass{
    static public function generateLocalisedContent($s_Stuff){
        return 'Your stuff is ' . $s_Stuff . PHP_EOL;
    }
}

try {
    $o1 = new sub1('Stuffed1');

}
catch(Exception $ex){
    echo $ex->getMessage(), PHP_EOL;
}

try {
    $o2 = new sub2('Stuffed2');
}
catch(Exception $ex){
    echo $ex->getMessage(), PHP_EOL;
}
?>


And I can now see an interface is a much simpler mechanism!


-- 
Richard Quadling
Twitter : @RQuadling
EE : http://e-e.com/M_248814.html
Zend : http://bit.ly/9O8vFY

--- End Message ---

Reply via email to