Đây là lỗi rất mới, xảy ra ở PHP 5.3.7 và 5.3.8
Trước khi đến với "is_a()", mình muốn giới thiệu với các bạn một chút về __autoload() (Autoloading Classes)
Autoloading Classes ra đời từ PHP 5, với mục đích để đỡ phải include quá nhiều ở đầu mỗi file PHP.
Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long javascript:void(0)list of needed includes at the beginning of each script (one for each class).
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
Example #1 Autoload example
This example attempts to load the classes MyClass1 and MyClass2 from the files MyClass1.php and MyClass2.php respectively.
<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
Và bây giờ là "is_a()" function.<?php
function __autoload($class_name) {
include $class_name . '.php';
}
$obj = new MyClass1();
$obj2 = new MyClass2();
?>
bool is_a ( object $object , string $class_name )
Checks if the given object is of this class or has this class as one of its parents
Example #1 is_a() example
<?php
// define a class
class WidgetFactory
{
var $oink = 'moo';
}
// create a new object
$WF = new WidgetFactory();
if (is_a($WF, 'WidgetFactory')) {
echo "yes, \$WF is still a WidgetFactory\n";
}
?>
Như vậy, is_a là hàm dùng để kiểm tra xem 1 object có là hoặc thuộc vào một class nào đó hay không.// define a class
class WidgetFactory
{
var $oink = 'moo';
}
// create a new object
$WF = new WidgetFactory();
if (is_a($WF, 'WidgetFactory')) {
echo "yes, \$WF is still a WidgetFactory\n";
}
?>
Giả sử có đoạn code sau:
<?php
function __autoload($class) {
echo "Would load: " . $class . PHP_EOL;
}
$var = "test";
var_dump(is_a($var, 'B'));
$obj = new Stdclass;
var_dump(is_a($obj, 'C'));
?>
Khi chạy kết quả chúng ta kỳ vọng sẽ là:function __autoload($class) {
echo "Would load: " . $class . PHP_EOL;
}
$var = "test";
var_dump(is_a($var, 'B'));
$obj = new Stdclass;
var_dump(is_a($obj, 'C'));
?>
bool(false)
bool(false)
Nhưng thực tế, kết quả lại là:bool(false)
Would load: test
bool(false)
bool(false)
Đó là do, khi hàm is_a() được gọi là tham số đầu tiên không phải là một object thì hàm __autoload() sự tự động được gọi. bool(false)
bool(false)
Với lỗi trên, nếu __autoload() được viết theo cách thông thường như ví dụ sau:
function __autoload($class_name) {
include $class_name . '.php';
}
$uploaded_file = File::readAll($uploaded_filename);
if (PEAR::isError($uploaded_file)){
print_error($uploaded_file);
}else{
process_upload($uploaded_file);
}
Thì website sẽ mắc lỗi RFIinclude $class_name . '.php';
}
$uploaded_file = File::readAll($uploaded_filename);
if (PEAR::isError($uploaded_file)){
print_error($uploaded_file);
}else{
process_upload($uploaded_file);
}
References:
http://www.securityfocus.com/bid/49754/solution
0 comments:
Đăng nhận xét