Читать книгу PHP This! A Beginners Guide to Learning Object Oriented PHP - Michelle Gosney - Страница 5

PHP Objects & Classes

Оглавление

At this point I would explain what an object is but you can't have an object without a class function. A class is essentially a blueprint for an object. It is a user-defined data type similar to a C-struct but more complex. A single instance of a class is an "object.”

A class defines characteristics that an object will have called properties. Properties describe an object and can be assigned values. For example, color is a property but “red” is a value assigned to color.

A class can also have member functions defined in them called methods. These functions manipulate the data values assigned to the properties within the class and control the behavior of the object created from the class. For example, a method can take a property value of an object, like the color of an object, and write it to the database, or visa versa.

Objects are at the very core of object-oriented programming so let me explain what an object is in the context of computer programming. An object is simply data that is structured according to a user defined template that is defined in a class function. Technically speaking, a class is a construct that defines constituent members and is used to create instances of itself. Simply speaking, a class function is essentially just a blueprint that defines the characteristics of an object, its properties, the way the object will behave, and its method functions. The properties describe and object and the methods process the data assigned to these properties.

An object can be almost anything you can imagine, a bird, a table, a house or a person. If the object is a person, then some common characteristics that can describe that person are name, gender, eye color, hair color, etc.

Before an object can exist it must have a class to define it. Our simple “Person” class will start off looking like this in PHP:


This is as basic as it gets and all this does is define our class. At this point, our class is empty. It has no properties to describe the objects that will be created from it nor any methods that will give its objects the ability to do things and process data. Let’s add some basic characteristics that may describe a person as mentioned above:


Here we defined some properties that help describe a person but we can’t do anything with these properties yet because we don’t have any methods to set the value of these properties or any methods to get the values. Let’s add some “setter” and “getter” functions to handle our properties. We will also add a method called __construct but we won’t do anything with it at this time, that is, we won’t implement it at this time. We’ll talk about it in detail in Chapter 3 when we cover PHP Magic Methods:



Now the Person class above contains some properties that describe a person and some methods to set and get that information. Notice how the variable “$this” was used, we’ll get back to it shortly.

Now that we have a person class, let’s create some person objects from that class. Allow me to introduce some new friends, Mindi and Billy, they can be two instances (two objects) of the Person class. To create Mindi and Billy, we must instantiate them.

PHP This! A Beginners Guide to Learning Object Oriented  PHP

Подняться наверх