English 中文(简体)
PHP、OOP、多形态
原标题:PHP, OOP, polymorphism

I am studying PHP and there is polymorphism example at the website i am studying at, The Link for the Code:Pastebin, What i didn t understand is why Class b and Class c after those classes that have been extended with the function hellow when they are already contain this function, i know that this is how the polymorphism works, and i am not sure but i remember some one told me that 2 functions with same name can be in one class becouse each function has it own signature, but has i say i am not sure if can some one help me to understand it please i will be very thankful.

最佳回答

您不是在谈论多形态,而是非常简单的遗产继承。 在一些 OOP 语言(如 Java ) 中,使用同名但有不同签名的多种方法确实可以实现,但PHP则不行,因为方法名称在某类中是独一无二的。

在您的情况下,类 b c < hello 方法将仅取代(超载)在 a 中已经定义的、其延伸的方法。

但是,从您的 b c 类中,您可能想要调用父类方法。这就是 a: hello($arg1, $arg2, date(Y-M-j)) 和 的方法。

问题回答

暂无回答




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签