Home  >  Article  >  Backend Development  >  What are the differences between php and c++

What are the differences between php and c++

藏色散人
藏色散人Original
2021-12-15 09:26:282052browse

The differences between php and c are: 1. PHP is a scripting language executed on the server side, and C is an inheritance of C language; 2. PHP does not support overloading in C; 3. About abstract It is different from the use of interface; 4. PHP is a weakly typed language, so the polymorphism of PHP is not as obvious as that of C, etc.

What are the differences between php and c++

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

What are the differences between php and c?

Difference:

A protected member variable (assumed to be i ) is of type Base, and its subclass Derived directly inherits from Base. At the same time, Derived has a member function func, and its parameters are of type Base. Can this function access the protected members of the Base object? It's not possible in C, but it is possible in PHP.

  • About overloading:

PHP does not support overloading in C. Redefine a function (whether the parameters are the same or different, will result in duplicate definition errors). PHP has its own overloading method, which is more extensive than that in C. It can not only overload functions (by using the __call() method), but also overload member variables (by using __get() and __set() ). In C, functions can only be overloaded, and the only methods for overloading are: different parameter types and different function modifiers (const).

  • About abstract and interface

There are these two modifiers in PHP because PHP does not support multiple inheritance of an ordinary class, so Interface is used to implement multiple inheritance. The Abstract class also exists in C, but the implementation method is different. The pure virtual function is used in C to indicate that this class is an abstract class and cannot be instantiated alone. In PHP, you can not only use the function modifier abstract (in addition, class must also be marked with abstract) to indicate this meaning, but you can also only use the abstract modifier to modify the class to more directly indicate that it is an abstract class. In addition, the pure virtual function in C is not restricted by the access type. No matter what the access type is, it will be regarded as public; while the abstract function in PHP cannot be declared as private, that's all.

  • Polymorphism

Because PHP is a weakly typed language, its polymorphism is reflected everywhere, resulting in its polymorphism Not as obvious as in C. For example, in PHP, the functions of the base class can be regarded as all virtual, so it does not need to add any modifiers. Functions in the subclass with the same name as the base class will be dynamically called, but it is different in C. If the function in the base class If this function does not add the virtual modifier, the function with the same name in the subclass will not be called dynamically, but can only be called statically.

  • Operator overloading

Does not exist in PHP, but does exist in C. The focus is on the == operator, which can be used on any type in PHP, even if the type does not have its own == overloaded function (like in C). For comparison of objects in PHP, == means that the attributes and values ​​of the two objects are the same, and the types are also the same; PHP also has a === operator, which means that they refer to the same object, which is very similar to Java. .

  • final keyword

This keyword exists in PHP, indicating that this function cannot be overridden (if he is used to modify the function), Or the class cannot be inherited (if class is modified with class). There is no such keyword in C, and that effect cannot be simulated.

  • Object assignment and copy

There are roughly three ways to copy or assign in C.

One is pointer assignment, that is, p1 = &obj, which is equivalent to the assignment operation in PHP and java;

The other is memberwise assignment, which occurs when obj1 = obj2 Things, by default perform shallow copy, and the effect of clone in PHP is the same. He can perform your deep copy or other customized copies by overloading the assignment copy operation, which is equivalent to the __clone() member function in PHP;

The third is memberwise initialization, that is It runs automatically when parameters are passed, when a return value is passed, or when the container containing the object is initialized. You can control its effect by defining the copy constructor yourself.

The copy constructor is almost never used in PHP because its transfer is completely by reference, not a direct copy of the object.

Introduction to PHP:

PHP (PHP: Hypertext Preprocessor) is a "hypertext preprocessor". It is a scripting language executed on the server side. It is especially suitable for Web development and can Embed in HTML. PHP syntax learned the C language, absorbed the characteristics of multiple languages ​​​​of Java and Perl to develop its own unique syntax, and continued to improve itself based on their strengths, such as Java's object-oriented programming. The main goal of the language was originally created to make Developers quickly write high-quality web sites. [1-2] PHP supports both object-oriented and process-oriented development, and is very flexible in use.

C Introduction:

C is the inheritance of C language. It can perform procedural programming of C language, object-based programming characterized by abstract data types, and oriented programming characterized by inheritance and polymorphism. Object programming. While C is good at object-oriented programming, it can also perform process-based programming. Therefore, C is big or small in terms of the size of the problem it can adapt to.

C not only has the practical features of efficient computer operation, but is also committed to improving the programming quality of large-scale programs and the problem description capabilities of programming languages.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the differences between php and c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn