|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-16 06:55 UTC] giorgio dot liscio at email dot it
Description:
------------
hi, i really can not understand why this was dropped, but imagine this code
please read carefully and please examine my request before trash it
abstract class FSItem
{
// abstract static method:
abstract protected function isPathValid($path);
protected function __construct($path){}
// random per-item singleton
public static function getByPath($path)
{
if(static::isPathValid($path)) // here is the static method call of classes. i want to check the path before instance it
return new static($path);
}
}
class Dir extends FSItem
{
//implementation:
protected function isPathValid($path)
{
return is_dir($path);
}
}
class File extends FSItem
{
//implementation:
protected function isPathValid($path)
{
return is_file($path);
}
}
class Image extends File
{
//implementation:
protected function isPathValid($path)
{
return (bool)getimagesize($path);
}
}
php changelog says:
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.
oversight what? it is a logical error? technical limitation?
thank you
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 02:00:01 2025 UTC |
sorry, corrected code: abstract class FSItem { protected function __construct($path){} // random per-item singleton public static function getByPath($path) { if(static::isPathValid($path)) // here is the static method call of classes. i want to check the path before instance it return new static($path); } // abstract static method: abstract protected static function isPathValid($path); } class Dir extends FSItem { //implementation: protected static function isPathValid($path) { return is_dir($path); } } class File extends FSItem { //implementation: protected static function isPathValid($path) { return is_file($path); } } class Image extends File { //implementation: protected static function isPathValid($path) { return (bool)getimagesize($path); } }hi Rasmus, what an honor! can you please give me some example code? you are saying that self:: points to an abstract not-implemented method? for example: abstract class cA { static function A(){self::B();} abstract static function B(); } in this case can be thrown an error, but using static:: the call refers to the called class method, not the declaring class static function A(){static::B();} so it can be re-enabled in this case, no? in php static methods are really powerful unlike java's, c#'s, don't limit them! why interfaces allows static abstract methods? how self:: is resolved?i know, but: abstract class cA { //static function A(){self::B();} error, undefined method static function A(){static::B();} // good abstract static function B(); } class cB extends cA { static function B(){echo "ok";} } cB::A();What's not allowed? abstract class cA { static function A(){static::B();} abstract static function B(); } class cB extends cA { static function B(){echo "ok";} } cB::A(); This works fine. You obviously can't call self::B(), but static::B() is fine.ok it is a php bug, it is related to files and namespaces... if you put this in global space works (no error triggered) <?php // index.php ini_set("display_errors", true); ini_set("error_reporting", E_ALL | E_STRICT); require("FSNuovo/Directory.php"); ?> <?php // FSNuovo/Directory.php namespace FSNuovo; abstract class AbstractFileSystemItem { abstract static function ensurePathIsValid($fullPath); } class Dir extends AbstractFileSystemItem { static function ensurePathIsValid($fullPath){} } ?>so, felipe says that is not a bug and hes right rasmus you say that this code is correct, but i still get a strict warning abstract class cA { static function A(){static::B();} abstract static function B(); } class cB extends cA { static function B(){echo "ok";} } cB::A();simplified case abstract class AFSItem { public static function getIfValid ($fullPath) { // i use static::isValid to get the method defined in the called class if(static::isValid($fullPath)) return new static($fullPath); } protected function __construct ($fp){} // i want to force real classes to implement a way to check a path before instance an object protected abstract static function isValid ($fullPath); // abstract declaration } class File extends AFSItem { protected static function isValid ($fullPath) // implementation { return is_file($fullPath); } } class Dir extends AFSItem { protected static function isValid ($fullPath) // implementation { return is_dir($fullPath); } } class Image extends File { protected static function isValid ($fullPath) // implementation with override { if(parent::isValid($fullPath) AND (bool)getimagesize($fullPath)) return true; return false; } }