6

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
  • I may be wrong, but don't dynamic languages such as Ruby or Python have their JIT compiler determine what type an object is at runtime? So while you may not explicitly see it's object oriented, when you run the program it actually is. If I'm wrong, please let me know. :)
    – sergserg
    Aug 21, 2012 at 2:29
  • @Serg You confuse language and implementation. Also, JIT compilation is a mere implementation detail, and one with no observable effect (I'm talking about semantics, not performance) to boot. But yes, any implementation of a dynamic language has to track the types of values somehow. Regardless of object orientation.
    – user7043
    Aug 21, 2012 at 4:51
  • 1
    The term "loosely"-typed is not well defined. There are two distinct qualities of type systems: Strongly typed (e.g. Java, Python) vs. weakly typed (e.g. C) and statically typed (e.g. C, Java) vs. dynamically typed (e.g. Python, PHP). Usually anything dynamically typed has to also be strongly typed, though I am not sure PHP does not have some functions that would violate it, since it's bindings to the C library are quite thin and rough.
    – Jan Hudec
    Aug 21, 2012 at 8:03
  • 1
    Follow-up: Having noticed the Don't describe type systems as "weak". in the other comment, "weak" and "strong" are not really well defined either. The "static" vs. "dynamic" is the main distinction that is well defined. By "weak" vs. "strong" I meant whether it allows casting in a way that may cause undefined behaviour at runtime (like casting number to pointer in C).
    – Jan Hudec
    Aug 21, 2012 at 8:26
  • 1
    can dynamically typed language be truly OO - yes, can PHP be considered truly OO - no.
    – vartec
    Aug 23, 2012 at 9:42

4 Answers 4

17

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
  • 3
    I agree. It's a classic logical fallacy: "C++, C# and Java are OO languages. C++, C# and Java have strict type checking. Therefore, OO languages require strict type checking". Aug 21, 2012 at 7:36
  • 1
    Note that Alan Kay, who after all invented the term "Object-Oriented" and thus gets to decide what it means has this to say: "I invented the term object-oriented and I can tell you I did not have C++ in mind". Aug 21, 2012 at 10:57
8

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
  • 2
    Don't describe type systems as "weak".
    – user7043
    Aug 21, 2012 at 4:52
  • @delnan Interesting link, thanks. But, one mention and I get scolded? I only mentioned it because even Wikipedia's article is titled weak typing. :(
    – Nicole
    Aug 21, 2012 at 6:41
  • @delnan By the way, if you follow my link you'll see it is to an answer of mine where I too tried to clear up some confusion in the term.
    – Nicole
    Aug 21, 2012 at 6:43
  • @delnan (Sorry for the multiple comments, was reading and digging up links) Lastly, I think Lippert's answer is food for thought but is generally of the vein "if we can't define every term to a perfect standard then conversation is futile" which I find a bit extreme. I think the various (1, 2) Wikipedia articles are a more fair and useful assessment for typical conversation.
    – Nicole
    Aug 21, 2012 at 6:49
  • 1
    I don't expect a Q&A site post to strictly define everything, and I don't mean to disrespect your answer (in fact I concur and think it's very well-written, +1). I'm just promoting, whenever I can, the idea of collectively abandoning a term which is far more likely to cause confusion. The asker of the question I linked to is not the first to be confused, and there has been more than one heated debate (and holy wars too) about which language is strongly typed or not. Just ask any militant Haskeller how "strong" C#'s type system is. Or ask around whether Python has strong or weak typing.
    – user7043
    Aug 21, 2012 at 6:57
5

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
  • 1
    Simula had already introduced the concepts in object oriented programming and was an influence on Smalltalk.
    – harald
    Aug 21, 2012 at 8:44
  • +1 for "don't check types, just call methods". well done, this allows a far better polymorphism than mere inheritance.
    – Javier
    Aug 21, 2012 at 18:58
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.