Is PHP Object-oriented?

2020-05-11 10:45发布

问题:

Is PHP an object-oriented language? If not, then what about the framework CakePHP? Is it an object-oriented MVC implementation of PHP?

Also, can a PHP application wholly built using classes be called object-oriented?

回答1:

Yes, the latest versions of PHP are object oriented. That is, you can write classes yourself, use inheritance, and where appropriate, the built in functionality is built in objects too (like MySQL features).

There are still a lot of loose functions however, so there might be a disagreement about how object oriented PHP is. I think it is. And yes CakePHP is an object oriented framework.



回答2:

No, PHP is not fully object oriented language.

And neither is C++ or Java, because they all have primitive types (and PHP also has a huge collection of function like str_replace() and is_*(), which are clearly procedural in nature). Only pure object-oriented language, that i know of, are Ruby and Scala (and one could argue that latter is more aiming at functional programming paradigm).

PHP is, what one could call, "object-capable language".

As for the code written in PHP, you have to understand that just because you are using classes, it does not make it OOP. Especially if your code is mostly based on static class.

So, if you ask: "is CakePHP an OO framework?", then the answer is - NO. The most flattering description for it would be "class oriented programming". The code-base is filled with static methods and variables, where class acts more like a namespace. Basically CakePHP is a procedural code, wrapped in syntax, which on surface mimics object oriented code.



回答3:

PHP is not fully object oriented but it supports some feature like

1) class
2) object
3) Constructors and Destructors
4) Object Inheritance
5) Scope Resolution Operator (::)

and many more. If you want to learn object oriented php refer the below reference link

1) http://php.net/manual/en/language.oop5.php



回答4:

For any language to be classified as Object Oriented it needs to adhere to at least 3 principles of OO: Inheritance, Polymorphism, and Encapsulation. I don't know PHP and I have never used it before but if it at least meets these 3 principles then it is OO otherwise it is not. My suggestion is to search for how PHP supports, or not, each principle. Developers tend to be overly attached to the language they currently use and often are misguided and biased. Take for example the person stating that C++ isn't an object orient programming and yet receiving many thumbs up! This person could not be further from the truth yet his answer appears to be correct to many!

So I did a bit of digging around and it seems that PHP5 supports these 3 principles!



回答5:

Interesting Question. But "No", PHP is a Object capable Language not Object Oriented, yeah but CakePHP is an Object oriented Framework.



回答6:

Object-Oriented technology is often described in terms of encapsulation, polymorphism, and inheritance. But these are only identity. If object-oriented technology is to be successfully it must emphasis on the object.

objects -- packet containing data and procedures

methods -- deliver service

message -- request to execute a method

class -- template for creating objects

instance -- an object that belongs to a class

encapsulation -- information hiding supported by objects

inheritance -- mechanism allowing the reuse of class specifications

class hierarchy -- tree structure representing inheritance relations

polymorphism -- to hide different implementations behind a common interface

[Alan Kays][4]["Considered by some to be the father of object-oriented programming"] Defination:

  1. EverythingIsAnObject.

  2. Objects communicate by sending and receiving messages (in terms of objects).

  3. Objects have their own memory (in terms of objects).

  4. Every object is an instance of a class (which must be an object).

  5. The class holds the shared behavior for its instances (in the form of objects in a program list)

An object is defined by a triple (OID, type constructor, state) where OID is the unique object identifier, type constructor is its type (such as atom, tuple, set, list, array, bag, etc.) and state is its actual value.

Now clearly it can be seen Java,C++ and PHP violates rule 1?Why bcoz int, float etc. (there are a total of eight primitive types). so it cannot be Object oriented in strict sense but some folk's considered it as OOP.



回答7:

You will find all of the latest OOP features in PHP from version 5. Before PHP v5, there was PHP v4, which was not fully Object Oriented.

Also you will find many new & advanced frameworks like the following:-

  • Magento
  • Zend
  • Kohana
  • Symphony
  • CodeIgniter
  • Yii

These frameworks have some great features & are really powerful in true sense, and some are also programmer-friendly.

Some notable points:-

  • PHP v5 still does not support Multiple Inheritance.
  • PHP v5 still supports procedural way of coding, so it is still backward compatible for older websites (which had been developed in procedural way using PHP v4).

Hope it helps.