A lot of people in other comments wanting to get the classname without the namespace. Some weird suggestions of code to do that - not what I would've written! So wanted to add my own way.
function get_class_name[$classname]
{
if [$pos = strrpos[$classname, '\\']] return substr[$classname, $pos + 1];
return $pos;
}
?>
Also did some quick benchmarking, and strrpos[] was the fastest too. Micro-optimisations = macro optimisations!
39.0954 ms - preg_match[]
28.6305 ms - explode[] + end[]
20.3314 ms - strrpos[]
[For reference, here's the debug code used. c[] is a benchmarking function that runs each closure run 10,000 times.]
c[
function[$class = 'a\b\C'] {
if [preg_match['/\\\\[[\w]+]$/', $class, $matches]] return $matches[1];
return $class;
},
function[$class = 'a\b\C'] {
$bits = explode['\\', $class];
return end[$bits];
},
function[$class = 'a\b\C'] {
if [$pos = strrpos[$class, '\\']] return substr[$class, $pos + 1];
return $pos;
}
];
?>
Use CONST to set UPPER and LOWER LIMITS
If you have code that accepts user input or you just need to make sure input is acceptable, you can use constants to set upper and lower limits. Note: a static function that enforces your limits is highly recommended.. sniff the clamp[] function below for a taste.
________số 8_______
public $width, $height;
public function __construct[$w = 0, $h = 0]{
$this->width = self::clamp[$w];
$this->height = self::clamp[$h];
}
public function __toString[]{
return "Dimension [width=$this->width, height=$this->height]";
}
protected static function clamp[$value]{
if[$value < self::MIN] $value = self::MIN;
if[$value > self::MAX] $value = self::MAX;
return $value;
}
}
echo [new Dimension[]] . '
';
echo [new Dimension[1500, 97]] . '
';
echo [new Dimension[14, -20]] . '
';
echo [new Dimension[240, 80]] . '
';
?>
- - - - - - - -
Dimension [width=0, height=0] - default size
Dimension [width=800, height=97] - width has been clamped to MAX
Dimension [width=14, height=0] - height has been clamped to MIN
Dimension [width=240, height=80] - width and height unchanged
- - - - - - - -
If you have code that accepts user input or you just need to make sure input is acceptable, you can use constants to set upper and lower limits. Note: a static function that enforces your limits is highly recommended.. sniff the clamp[] function below for a taste.
0
Việc triển khai các không gian tên của PHP bị ảnh hưởng bởi bản chất động của nó với tư cách là một ngôn ngữ lập trình. Do đó, để chuyển đổi mã như ví dụ sau thành mã được đặt tên
Ví dụ #1 Tự động truy cập các phần tử
class classname
{
function __construct[]
{
echo __METHOD__,"\n";
}
}
function funcname[]
{
echo __FUNCTION__,"\n";
}
const constname = "global";
$a = 'classname';
$obj = new $a; // prints classname::__construct
$b = 'funcname';
$b[]; // prints funcname
echo constant['constname'], "\n"; // prints global
?>
Ví dụ #2 Tự động truy cập các phần tử được đặt tên
namespace namespacename;
class classname
{
function __construct[]
{
echo __METHOD__,"\n";
}
}
function funcname[]
{
echo __FUNCTION__,"\n";
}
const constname = "namespaced";
/* note that if using double quotes, "\\namespacename\\classname" must be used */
$a = '\namespacename\classname';
$obj = new $a; // prints namespacename\classname::__construct
$a = 'namespacename\classname';
$obj = new $a; // also prints namespacename\classname::__construct
$b = 'namespacename\funcname';
$b[]; // prints namespacename\funcname
$b = '\namespacename\funcname';
$b[]; // also prints namespacename\funcname
echo constant['\namespacename\constname'], "\n"; // prints namespaced
echo constant['namespacename\constname'], "\n"; // also prints namespaced
?>
Hãy chắc chắn để đọc