views:

141

answers:

3

I know PHP 5 has some object oriented similarities but it's not a true OOP environment still right? Also does it have a true compiler? I see compiling of scripts which still means procedural. I assume it's not a real compiler in that any PHP compilers out there do not create assemblies?

+1  A: 

Similar questions discussed a lot of times. There is no "true" or "false" OOP. Due to php supports encapsulation, polymorphism, inheritance - it is Object Oriented Programming Language. And there are no still "true" compilers. But you can look at Facebook's Hip-Hop

zerkms
+6  A: 

PHP is now a completely object oriented language, even if most of the API isn't.

  • It supports class and objects.
  • It follows the principles of OOP (Inheritance, Encapsulation, Abstraction, Polymorphism)

It is therefore a completely object oriented language.


PHP actually does compile (by default on every run unless using "an accelerator") its scripts into intermediary byte-code which is then run by the Zend Engine.

It's actually pretty close to other languages:

  • VB.NET / C# / F# / other .NET Languages
    Those languages when compiled does not output binaries in assembly code, but rather binaries in Common Intermediate Language (CIL). CIL bytecode is then interpreted at runtime by the .NET Virtual Machine.

  • Java
    Java compiles .class and .jar files which are not in assembly code, but rather in Java Bytecode. Java Bytecode is then interpreted at runtime by the Java Virtual Machine

  • PHP
    PHP compiles into Zend Bytecode, which is then interpreted at runtime by the Zend Engine.

Andrew Moore
+7  A: 

I think you should read up a bit on the definition of the words you're using!

"Assemblies" are a word that .NET uses for something that's loosely translated as "a DLL plus some support stuff that you can deploy." Perhaps you were thinking about "assembly code"?

Compilers compile to all kinds of representations. JVM bytecodes, CLR bytecodes, x86 bytecode, Python bytecode, MIPS bytecode, ARM bytecode, ... Those are all valid targets for a compiler. Note that, for both JVM and x86 bytecode, there exists both hardware (CPU) and software (interpreter) execution environments, so whether the target code is "hardware" or not doesn't really play into it.

Compiling of code versus interpreting of code doesn't mean anything when it comes to OO vs procedural vs functional. OO has to do with supporting polymorphism, data hiding, data-goes-with-interface-implementations and composability.

PHP supports all those things, so you can use it to implement an OO design in a straightforward way, so I would say that PHP supports OO.

Finally: you should look at the Facebook PHP compiler, which compiles PHP to C and then C to x86, which apparently gives it a 50% speed-up compared to the traditional PHP execution environment.

Jon Watte
Assemblies require assembly code, same damn thing.
CoffeeAddict
Yes, I am asking about OOP. Polymorphism and all the rest. I know what OOP entails and the benefits of it and use thereof.
CoffeeAddict
@coffeeaddict: Actually, .NET assembiles are NOT compiled in Assembly. They are in compiled in CIL bytecode which is then interpreted by the .NET Virtual Machine. To quote you, they don't "require assembly code". In fact, there is no "assembly code" involved at all.
Andrew Moore
ok but assembly and CIL are same concept.
CoffeeAddict
ok but does PHP enforce certain usages to ensure it's true OOP? You say OOP Design. I do not know what you are referring to in that context as compared to a Java or .NET environment.
CoffeeAddict
@coffeeaddict: Not at all. Assembly code is basically pure processing instructions sent directly to the CPU. Assembly is platform and architecture dependent. CIL bytecode (or any bytecode for that matter) is a set of instructions intended for a virtual machine which then sends proper instructions to the CPU based on the home platform and architecture. The same binary can therefore be deployed on multiple platforms without any changes (dependencies aside). Which is also why one .NET binary can target both x86 and x64, but a C++ program (compiled in assembly) requires two separate binaries.
Andrew Moore
Is that rhetorical? PHP doesn't enforce much at all - types, OOP, what have you.
ceejayoz
@coffeeaddict well PHP follows exactly the same CIL way. But what it have to do with procedural/OOP?
Col. Shrapnel
@coffeeaddict: C# doesn't enforce OOP, VB.NET doesn't enforce OOP, Java doesn't enforce OOP. I could simply have one class with a bunch of functions and have basically procedural code (or in the case of VB.NET, you could even have one module). As a programmer, you have to decide which way to structure your program. No programming language can force that on you. And the fact that it compiles into assembly or bytecode doesn't change anything.
Andrew Moore
To continue on this, the problem with PHP is not the language itself. It's the low education of most programmers using it which gives the language such a bad reputation. Most of the books out there for PHP are absolutely terrible. So are most of the examples out on the web. A good structured site in PHP can be as object oriented as a good structured site in ASP.NET.
Andrew Moore
C# doesn't enforce OOP. Um, Java and C# enforce rules that help to ensure certain things in an OOP environment. Sure it doesn't force you to create abstract classes, Interfaces, or proper OOP overall, But there are controls involved. I wonder about PHP.
CoffeeAddict
just like ceejayoz is saying, it does not enforce types, It's a loose language which is just one example of many checks and balances in a true OOP language. So I guess I got my answer here...it still is untyped, etc. Even in that Facebook link zerkms posted http://developers.facebook.com/blog/post/358, they addressed those very issues with PHP not being a true OOP language at the heart:
CoffeeAddict
@coffeeaddict: Being strongly-typed or weakly-typed does not make the language less object-oriented. I starting to have the distinct feeling you asked the question to start a fight. **PHP supports all principles of OOP therefore is an object-oriented language.** C# does not prevent me from creating a completely procedural application. Neither does Java. Instead of creating objects and classes, I can create `structs` in both C# and Java. In PHP, I can use arrays instead of objects and classes. It's up to the programmer to use the OO features in his language of choice.
Andrew Moore
@coffeeaddict PHP is **not untyped**! And weak typing != OOP != compilation. These are all completely unrelated concepts, a language can be any one of these without the others.
deceze
Also, **PHP has types... It is not untyped**. C# starting 4.0 can also have weakly-typed varibles via the keyword `dynamic`. Does it make it less of an object-oriented language? In PHP, all variables are `dynamic`, which means I can store a `string`, then store an `integer` in the same variable. But at one given moment, a variable is one type. and one type only. Which is why `1 !== "1"` and things like `is_int()` and typecasting by doing `(int)` exists. I can also typecast a specific object of a class (ie.: `Chair`) into a superclass (ie.: `Furniture`) just **like any other OO language**.
Andrew Moore
As a disclaimer, I am a PHP developer. I also do Windows applications in C# and develop mobile and cross-platform applications in Java. Everything I can do that is object-oriented in C# and Java, I can do in PHP.
Andrew Moore
And interestingly enough, Hiphop still supports PHP's weak-typing, even if you saw that as "not being object-oriented".
Andrew Moore