PHP This! A Beginners Guide to Learning Object Oriented PHP. Michelle Gosney

Читать онлайн книгу.

PHP This! A Beginners Guide to Learning Object Oriented  PHP - Michelle Gosney


Скачать книгу
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.

      Instantiation

      Let’s start with the term “instantiate.” To instantiate a class means to create an object based on that class, or to create an instance of that class. In PHP the way to do this is by using the new keyword like this:

      $Mindi and $Billy are now both instances (objects) of the class Person so their properties can be assigned specific values. Notice how I used the parenthesis after Person. This is because of the constructor I defined in the Person class, the first function just after the properties are defined. This constructor is not implemented in this example and I will explain this in detail in Chapter 3 when I discuss Magic Methods.

      Let’s start with Mindi. We all know about her tattoo but let’s officially set her name and gender and some other attributes:

      Now for Billy:

      $this Variable

      The $this variable is a pointer to the object making the function call. That may not mean much to you so let’s explain it as it applies to Mindi. In the movie “Weird Science” two geeks created a metaphysical hot chic. That’s what we did when we instantiated $Mindi. Her blueprint was the Person class. We gave her dazzling blue eyes with the following method:

      That method is defined in the Person class like so:

      So $this was replaces with $Mindi at the moment $Mindi was instantiated because $Mindi is a copy of the Person class except with specific attributes instead of generic ones like $this. Hence the $Mindi object sees the setEyeColor(‘blue’) method like this:

      Access Modifiers

      Access Modifiers are keywords that control visibility of class properties and methods. In PHP there are three of them:

      1. Private

      2. Protected

      3. Public

      Let’s describe Mindi in terms of her own class and let’s say that Mindi has a tattoo on her wrist. We could put it in PHP terms like this:

      When we do it this way, the tattoo is public by default. That means it can be seen anywhere in the program.

      It's the same as writing it as:

      This means class Billy or class Alex or class Dad can all see Mindi's tattoo because it has public visibility which is set by a public access modifier.

      Now let's say Mindi instead has a tattoo that she doesn't want class mom or class dad to know about but wants to show off to her friends. She has this tattoo on her back so she can display it wearing a bikini top.

      Now Mindi can wear her bikini top around her friends and not her mom or dad. We can describe this in PHP by extending an invitation to the people she wants to show it to by using the extends key word:

      Since all of these classes extend class Mindi, they can see her tattoo. This is inheritance and all of these classes that extend Mindi are part of her inheritance hierarchy.

      Now let's say she has her tattoo in a more intimate place (use your imagination) and she only wants her boyfriend Billy to see it and no one else. In PHP terms:


Скачать книгу