Video: Abstracting classes

Visibility scope is not the only object-oriented way to restrict access. PHP5 also introduced the concept of abstract classes and methods. If a class is defined as abstract, it cannot be instantiated. If you define a method as abstract, then any class that extends that abstract class containing the method must also declare a method with the same name and arguments. Further, if a class has an abstract method, then the class itself must be abstract.

Whether you're enhancing or optimizing existing code or just starting from scratch, there's never a better time to start integrating object-oriented design techniques. This course shows how to integrate the principles of object-oriented programming into the build of a PHP-driven web page or application. After an overview of what objects and classes are and why they should be used, author Jon Peck dives into creating and instantiating objects, then defining the class relationships and interactions that will form the basis of your coding arsenal. The course also shows how to leverage PHP objects and implement design patterns, and looks at steps you can take to continue adding to your programming tool belt.

Topics include:

Historical overview of object-oriented PHP

Defining classes

Creating a method/object context with $this

Accessing classes without instantiation

Creating a database class

Extending and abstracting classes

Cloning and comparing objects

Error handling with exceptions

Implementing design patterns, such as the factory and strategy patterns

Abstracting classes

Visibility scope is not the only object-oriented way to restrict access.PHP5 also introduced the concept of abstract classes and methods.If a class is defined as abstract, it cannot be instantiated.If you define a method as abstract, then any class that extends thatabstract class containing the method must also declare a method with thesame name and arguments.Further, if a class has an abstract method, then the class itself must be abstract.

This solves the problem of the generic address class, and provides a mechanism tospecify the behavior of any child classes.Open the address class, scroll to the top, and add the word "abstract" at thebeginning of the class declaration.Save the address, then switch to your browser, and refresh the demo.At the bottom, the attempted instantiation of the generic address class has beendisallowed, and caused a fatal error.This is a good thing, as it forces the developer to be specific.Edit the demo, and re-factor the second address instance to be a business address,including the variable name. AddressBusiness. address_business for the variablename as well. And then, add a var_ export at the end, so we can inspect thecontents. Save the demo, go back to the browser, and refresh.

The business address is now displayed correctly.However, there is a data inconsistency; it's the correct class (AddressBusiness),but the _address_type_id is not set.Missing property values is a common development problem.When working with multiple similar objects, there can be a lot of easilyforgotten details to keep track of.The solution is to initialize objects automatically, so you don't have toremember to do it, but what's the best way to initialize them?Your first reaction may be to make the construct magic method abstract. Thiswould technically work, but you'd end up with a lot of copied and pasted code, aseverything in construct would have to be duplicated.

This can easily lead to a fragile system, where re-factoring or adding newfunctionality becomes cumbersome.It's not a best practice, and not very object-oriented.Instead, I'm going to construct the object in the parent, then call anabstract initializer.This way, common behavior in the parent's construct method always executes uponinstantiation. Then, custom behavior can be put in each child'sinitialization method. Neat!With these relationships, visibility is also a factor, and abstract methodvisibility can be a bit tricky.

Methods that implement an abstract method will also need to have the same scope,or something a little bit less restrictive.If an abstract method is declared as public, I won't be able to change it to private.However, the opposite is true.I can make an abstract private method public.It's a one-way change.You can relax the scope restriction, but not make it stricter.This prevents a situation when you're expecting to be able to do something, butfor some strange reason, you are prevented by scope.

To practically apply this to the addresses, I'm going to require that extendingclasses set the address type id upon creation.Let's open the Address class.After the magic method toString, define a new abstract protected method calledinit, abstract protected function _init().Force extending classes to implement init method, then add a call to theinitialization method at the beginning of the constructor. $this->init. Save theaddress class, then go to your browser, and refresh.

You will see an immediate error.The AddressResidence class contains an abstract method that needs to be defined.So, open the AddressResidence class, and declare the private function initialized.protected function _init(). Set the address type id to the constant from theaddress class, using your validating method:$this-> setAddressTypeIDAddress::ADDRESS_TYPE_RESIDENCE.Add some documentation, and copy the contents of this method, and paste it intothe business address. ADDRESS_TYPE_ BUSINESS. And, again for the Park. Save, thenreturn to your browser, and refresh.

You'll see that the _address_type_id for the business is now set correctly.However, a redundancy has now been introduced with the potential for corruption.You can still set the _address_type_ id manually, and if you look at the demo,AddressResidence is doing just that.You can just remove the _address_type_id, but that treats the symptom, not the problem.Return to the address class, and navigate to the magic set method.Remove the special case for _address_ type_id. Save, then refresh the demo.

You will see a notice for undefined or unallowed property.Data corruption is now prevented.Edit the demo, and remove the now- broken attempt to set the _address_type_id.Then save, and refresh.The notice is now gone, and execution is error-free.In the next segment, I'm going to demonstrate how to implement a commoninterface across all these child address classes without having to know whatkind of an address type it is.

Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .

Here are the FAQs that matched your search "" :

Expand all | Collapse all

please wait ...

Q: I'm not seeing warnings or errors in my environment like the video; why not?

A: Your PHP configuration is probably configured not to show them to you. This is often true on commercial web hosts and is often the default. Fortunately, there are multiple ways of resolving this.

The easiest way would be to explicitly enable error reporting at the top of the PHP script you wish to debug.

error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 1);

Alternatively, if you have access to your php.ini file and you want to always have error reporting on, change error_reporting = to a development friendly value of

error_reporting = E_ALL | E_STRICT

then change display_errors = to

display_errors = On

Finally, if access to the php.ini file is not available, you can add the following directives to your .htaccess or VirtualHost configuration for Apache:

php_value error_reporting 32767php_value display_errors 1

If you would like a development optimized development environment like the one utilized in this course, see Up and Running with Linux for PHP Developers, here in the lynda.com online training library.

Q: This course was updated on 4/10/2013. What changed?

A: The author rerecorded some of the tutorials to add more background information and better graphics.

Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.