Для чего перегрузка функции PHP?

Base64 только использует алфавитно-цифровые символы и '+' (плюс), '/' (наклонная черта) и '=' (равняется). Никакая потребность закодировать что-либо для XML.

19
задан Click Upvote 2 October 2009 в 23:36
поделиться

2 ответа

PHP's meaning of overloading is different than Java's. In PHP, overloading means that you are able to add object members at runtime, by implementing some of the __magic methods, like __get, __set, __call, __callStatic. You load objects with new members.

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are обработанные магическими методами можно установить в классе для различных типы действий.

Пример:

class Foo
{
    public function __call($method, $args)
    {
        echo "Called method $method";
    }
}

$foo = new Foo;

$foo->bar(); // Called method bar
$foo->baz(); // Called method baz

И, кстати, PHP поддерживает этот вид перегрузки, начиная с PHP 4.3.0. Единственное отличие состоит в том, что в версиях до PHP 5 вам нужно было явно активировать перегрузку с помощью функции overload () .

26
ответ дан 30 November 2019 в 03:20
поделиться

If you want to overload a function like in Java, don’t specify any arguments and use the func_num_args and func_get_args function to get the number of arguments or the arguments themselves that were passed to that function:

function test() {
    $args = func_get_args();
    switch (count($args)) {
        case 1:
            // one argument passed
            break;
        case 2:
            // two arguments passed
            break;
        default:
            // illegal numer of arguments
            break;
    }
}
15
ответ дан 30 November 2019 в 03:20
поделиться
Другие вопросы по тегам:

Похожие вопросы: