Defining a class
Video: Defining a classIn this chapter, we're going to create an object, which will be an instance of an address class. I'll start by describing how to define a class and its properties. Then, we'll add a behavior to the class with a method. I'll show you how to create an object, and access its contents, then finally, how to protect those contents from the rest of the program. Throughout this course, we'll be building upon this address class. So, remember to save often as you progress. Ready to start coding? Switch to your editor, and create a new file.
Viewers: in countries Watching now:
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.
- 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
Defining a class
In this chapter, we're going to create an object, which will be an instance of an address class. I'll start by describing how to define a class and its properties. Then, we'll add a behavior to the class with a method. I'll show you how to create an object, and access its contents, then finally, how to protect those contents from the rest of the program. Throughout this course, we'll be building upon this address class. So, remember to save often as you progress. Ready to start coding? Switch to your editor, and create a new file.
The same PHP file-naming conventions apply to files that contain classes. Many coding standards have file naming and content conventions as well. For readability, let's name the file containing the example class something logical. class.Address.inc. I'm using the extension INC, short for include, to indicate that this file is intended to be included, and not executed on its own. Class definitions always start with the keyword "class" followed by the name of the class.
There are three PHP rules regarding the naming convention used for classes. First, the class name must start with a letter or underscore. Then, any remaining characters must only be letters, numbers, or underscores. Finally, there's no limit on the length of the class name. There are a number of best practices for naming a class to make reading and sharing code easier. Conventions differ between coding standards, but a common theme tends to be the use of uppercamel naming, meaning the first letter of each word is capitalized. In contrast, it's best to name variables in lowercase, which makes them easy to tell apart.
Finally, it's best practice to avoid the name "class" when naming a class, because it's frankly confusing. Following the class name is a pair of curly braces that enclose any properties and methods that belong to the class. PHP allows the contents of the class to be empty, so I'll get into greater detail about both properties and methods in a moment. First, I want to demonstrate some good and bad class names. Here are some practical examples of class creation in PHP. class Address.
This is valid, as it starts with a letter, and the remaining characters are also letters. class PhysicalAddress. And, class Physical_Address. Each of these is valid, and follows common best practices for capitalization. The use of underscores for word separation differs between coding standards. class physical_address, but this time in lowercase.
This is valid, but not a best practice as each word does not start with a capital letter. class 21_jump_street. This class name is invalid. It will trigger a parse error, expecting t-string, because it starts with a number. Now that we've got a way of defining a class, let's start populating it with properties.
Find answers to the most frequently asked questions about Object-Oriented Programming with PHP .
Here are the FAQs that matched your search "" :
- 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);
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 32767
php_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.