Can a loosely typed language be considered true object oriented?

Can a loosely typed programming language like PHP be really considered object oriented?

I mean, the methods don’t have returning types and method parameters has no declared type either.

Doesn’t class design require methods to have a return type?
Don’t methods signatures have specifically-typed parameters?

How can OOP techniques help you code in PHP if you always have to check the types of parameters received because the language doesn’t enforce types?

Please, if I’m wrong, explain it to me.

When you design things using UML, then code classes in PHP with no return-typed methods and no-type parameters… Is the code really compliant with the UML design?

You spend time designing your software architecture, then the compiler doesn’t force the programmer to follow your design while coding, letting he/she assign any object variable to any other variable with no “type-mismatch” warning.

5

The classic attributes of OOP are: inheritance, polymorphism and encapsulation. No mention of strong typing.

I do not know PHP, but I have coded OOP Python and it feels just like OOP C#.

2

Doesn’t class design need methods to have a return type?

Why? You aren’t modifying the code itself at runtime, so the method has a return type whether it’s predeclared or not. Just because it can’t be enforced doesn’t mean it doesn’t exist — it can clearly be inferred from the code itself.

Doesn’t methods signatures have specifically-typed parameters ?

Again, there’s nothing to say that the method can’t simply be liberal in what it receives. Get user by id? Sure, pass in a String or an integer. How does that break object-oriented-programming?

How can OOP techniques help you code in PHP if you have to always check the types of parameters received because the language doesn’t enforce types ?

Here, based on the above arguments, I again fail to see why “extra checking”, even if needed, renders OOP techniques powerless.

The loosely-typed languages that I am familiar with don’t have method overloading, which is often used to make methods more flexible on input (while still using the same name), so I fail to see much difference from using one method and “extra checking”. A diligent developer can write the same method, with the same contract, stability and security in a loosely-typed language.

When you design things using UML, then code classes in PHP with no return-typed methods and no-type parameters… Is the code really compliant with the UML design ?

Well this depends on your method itself, doesn’t it? What it actually does with the inputs and what it outputs?

Typing still exists

The bottom line is, types still exist, they are just loose or weak1, and these systems can even have certain advantages.

In fact, some IDEs like those by JetBrains (IntelliJ, PHPStorm) support all kinds of type hinting using PHPDocs for PHP. As I’ve used it, it provides almost all the convenient warnings, click-through, code-completion, etc. that an IDE for a strongly-typed can provide.


1: Both “loose” and “weak” are just a poor grouping anyway; there are so many different type systems that these terms are likely to cause confusion. See Eric Lippert’s informative perspective on this topic on Stack Overflow.


Method doc and inline example

/**
 * @static
 * @param mixed $id
 * @return User
 */
public function getUserById($id) {
    ....
}

public function printUserName() {
    // code completion provided on getName method because of @return
    $name = $this->getUserById("5")->getName();

    // type hinting for arrays
    /** @var User[] $users */
    $users = array(new User());
}

Casting example

public function checkUser(User $user) {
    if ($user instanceof Administrator) {
        /** @var Administrator $admin  */
        $admin = $user;
        $admin->logAccess();
    }
}

10

The original object-oriented language where Alan Key introduced the concept was Smalltalk. It is dynamically typed.

Actually Alan Key mentioned the classes and interfaces approach of C++ and Java is not what he had in mind when he invented object oriented programming. So the question should perhaps even be the other way, whether statically typed languages can be truly object oriented.

I mean, the methods don’t have returning types and method parameters has no declared type either.
Doesn’t class design need methods to have a return type? Doesn’t methods signatures have specifically-typed parameters?

No, they don’t have specifically typed parameters. Their parameters and return values have to have specific set of methods, but they don’t have to be specific types. In statically typed languages that is enforced by declaring an interface. In dynamically typed ones, you just document it.

How can OOP techniques help you code in PHP if you have to always check the types of parameters received because the language doesn’t enforce types?

You should never check types of parameters received. You should simply call methods on them. That’s called “duck typing” (if it quacks like a duck and walks like a duck, I would call it a duck, or if it has methods like X, it will do for X). It’s how things are usually done in smalltalk, perl, python, ruby etc. and as they should be done in PHP.

2

Doesn’t class design need methods to have a return type?

Not really.

Doesn’t methods signatures have specifically-typed parameters ?

Sure. Just because the types aren’t enforced by the compiler doesn’t mean that there’s not implied contract and semantics that the methods require/implement.

How can OOP techniques help you code in PHP if you have to always check the types of parameters received because the language doesn’t enforce types ?

By focusing on Object Oriented Programming. Again, how you organize the implied contracts of dynamic languages in your design is where OO comes into play.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật