What is object-oriented programming 10

10 Object orientation using the example: A web application with PHP 5 and Ajax

In this practical chapter, we will demonstrate the principles discussed using a specific application.

Galileo: Your book is almost sold out by now, and we should consider whether we can just reprint it or bring out a revised version with updated and new content. I know you're both pretty busy, but we at the publisher think a revised version would be a better choice.

Bernhard: We should smooth out a few rough edges and incorporate the readers' tips into the new edition. For example, we could program a small application in a practical chapter to demonstrate the object-oriented approach and the principles presented here.

Gregor: Good idea! For example, we could program a cool application for the iPhone. We could also introduce ObjectiveC, a beautiful programming language that, as an object-oriented successor to C, I simply like better than C ++.

Bernhard: Well, ObjectiveC is interesting, but it is hardly used. The Mac and the iPhone are elegant, but the majority of our readers do not program for MacOS X or the iPhone. I'm not sure if the chapter would be as useful for them then, too.

Gregor: What do we do then? Something in Java, Python, or Ruby? C ++ would be my last choice here.Galileo: The PHP language has been popular and widely used in web programming for years. Would that be a possible basis for the chapter with practical application?

Gregor: That works out well, in the current version 5 PHP now also has real object-oriented properties. This is a good illustration of a number of the concepts presented in the book.

Bernhard: Then let's write a practical chapter on object-oriented programming in PHP.

Gregor: And to make the sample application even more interesting, we're going to write a web application that makes extensive use of Ajax. Therefore, we will also focus on JavaScript practically.

And so this chapter came about ...

10.1 OOP in PHP

With programming languages ​​like Python or Ruby, one can argue about which of the languages ​​is more elegant - PHP is above any discussion of elegance. It was already established in the domain of web development when Ruby or Python didn't even exist. PHP emerged as a simple alternative to Perl for creating dynamic websites.

In the course of time PHP acquired the capabilities of more powerful programming languages, and since versions 4 and 5 PHP can be counted as part of the family of object-oriented programming languages.

PHP is still used very often in web programming (and almost exclusively there) because it is a simple, freely available and powerful programming language with very extensive libraries.

PHP was not designed as an object-oriented programming language from the start. The object-oriented capabilities did not appear until version 4. This implementation was completely replaced in version 5 in order to integrate more functionality and to greatly improve the performance of the object-oriented PHP programs. The word "replaces" is not entirely correct: Since the implementations of the object-oriented language components of PHP in versions 4 and 5 are not compatible, the old implementation in PHP 5 was retained so that old scripts could also run under PHP 5.

New scripts, if you want to program them object-oriented, should only use the new syntax introduced in version 5. Therefore, we will only devote ourselves to this syntax here.

10.1.1 Classes in PHP

Apart from the dynamic nature of PHP, the classes in PHP are similar to the classes in Java. PHP supports simple inheritance from implementing classes and multiple inheritance from explicitly declared interfaces.

Here is an example of a class declaration in PHP:

Listing 10.1Example of the syntax of a class in PHP

This example shows the basic elements of the syntax of a class in PHP. A class is declared with the keyword class.

Class and object variables, constants

Class and object variables are declared in PHP with their visibility level in front of them: "Public", "Protected" and "Private".

Since PHP is a dynamically typed language, there is no need to declare a data type. You can assign an initial value to an object variable, but this initial value must be constant and may not contain a function call, for example.

Public elements of an object can always be accessed, private elements only within the method of the class in which the elements were declared. The protected elements can be accessed by the method of the declaring class and the subclasses derived from it.

For example, to access the object variable $ value of an object assigned to the variable $ x, write $ x-> value. So you leave out the second dollar sign. However, $ x -> $ value would not be a syntax error, but would have a different meaning. $ x -> $ value would first evaluate the value of the local variable $ value and then access the public object variable with the evaluated name.

In contrast to object variables, the class variables are identified with the static keyword.

To access the class variable $ count of the class MyClass, write MyClass :: $ count [The operator :: is called Paamayim Nekudotayim in PHP. In Hebrew this means "double colon". ].

You can also declare class-specific constants using the const keyword. Unlike variables, constant names do not start with a dollar sign. To access the constant MAX of the class MyClass, write MyClass :: MAX.

Class and Object Methods

Methods are declared in the classes similar to global functions with the keyword function. The functions can also be assigned to the three visibility levels using the keywords public, protected and private. If no such keyword is given, the public visibility level is assumed.

If you want to access your own object variable within a method, you have to use the special reference. To access the private object variable $ id, you write. The notation $ id would point to a local method variable.

To access your own class variables or class constants, you can use the class reference self instead of the class name. So you can write self :: $ count instead of MyClass :: $ count.

Class methods can also be declared in PHP; they are identified with the static keyword, similar to class variables. The reference is not defined within a "static" class method, so there is no access to the object variables. Interestingly, you can also call any object method in a "static context". For example, you can write MyClass :: output () instead of $ x-> output (). Such a call is not an error, but it does not set the special reference. In this case the special reference remains unchanged, so that you can call an overwritten method on a superclass in the context of the current object in this way (similar to C ++). To call the method of the direct superclass, you can use the special class reference parent instead of the name of the superclass. [Since version 5.3, instead of the directly specified name of the class, you can also use a variable whose value is the name of the class. ]

Inheritance in PHP

The keyword extends indicates from which superclass the declared class is derived. The implements keyword introduces the interfaces implemented by the class.

A class must implement all the methods of the interfaces, or it must be declared abstract. As in Java, you cannot create instances of an abstract class.

The importance of abstract classes and methods as well as interfaces in PHP is less important than in Java. In PHP the variables do not have a fixed type, so that you do not need any explicitly declared interfaces to be able to call operations on an object.

The abstract classes and methods as well as the interfaces should be used in PHP in order to avoid programming errors of your own. At runtime, you can use the instanceof operator to check whether objects belong to certain classes and interfaces.

PHP knows final classes and methods. These are identified with the keyword final. Similar to other programming languages, final methods cannot be overridden in derived classes, and final classes cannot be inherited.

Life cycle of objects

Instances of a class are created with the keyword new. A class can declare a constructor method. The name of the method must be __construct [Alternatively you can use the name of the class, internally this name is converted to __construct. ] and can have any number of parameters. In contrast to programming languages ​​such as Java, C # or C ++, the constructor of the superclass is not called automatically. This must be done explicitly with the parent :: __ construct (...) call.

A class can also declare a destructor named __destruct. This cannot have any parameters. Similar to the constructor, the destructors of the superclasses are not called implicitly. The destructor is called automatically when the object can no longer be reached or when the script is terminated. You should be particularly careful with destructors - as soon as an exception is thrown in a destructor and not caught, the script is terminated with a fatal error.

10.1.2 Dynamic Nature of PHP

PHP is a dynamic programming language. The dynamic is not least characterized by the fact that object variables can also be created dynamically. For example, if $ x points to an instance of the class MyClass, the call will be

$ x-> description = "An example object"

does not cause an error, but creates a new object variable of the object $ x.

A little magic

How PHP behaves when you access nonexistent object variables or call nonexistent methods can be influenced with a few "magic" methods. If you have declared a method __get ($ name), this will be called as soon as a non-existent object variable with the name $ name is read. For write access, __set ($ name, $ value) is called if you have declared this method.

A similar "magic" is revealed when invoking nonexistent methods. Depending on whether the non-existent method is called in an object context (with set) or a static context, one of the "magic" methods __call ($ name, $ arguments) or __callStatic ($ name, $ arguments) is called.

If you want to learn more about this "magic", which is called overloading in PHP, we refer you to the very comprehensive manual of PHP (http://de.php.net).

your opinion

How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]