Defining class properties
Video: Defining class propertiesAs you may recall, a class property is a structure for storing values. A class can have any number of properties associated with it. Each property is defined with a visibility keyword: public, protected, or private. If you leave out the keyword, you're going to get a parse error. I'll explore visibility scope in greater detail in a later video. For the time being, let's just stick with public properties that have no access restrictions. Properties are declared kind of like regular variables, including the naming conventions for PHP labels. Same as class naming, property names must either start with a letter or underscore, then be followed by any number of letters, numbers, and underscores.
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 class properties
As you may recall, a class property is a structure for storing values. A class can have any number of properties associated with it. Each property is defined with a visibility keyword: public, protected, or private. If you leave out the keyword, you're going to get a parse error. I'll explore visibility scope in greater detail in a later video. For the time being, let's just stick with public properties that have no access restrictions. Properties are declared kind of like regular variables, including the naming conventions for PHP labels. Same as class naming, property names must either start with a letter or underscore, then be followed by any number of letters, numbers, and underscores.
When naming a property, a best practice is to use lowerCamel naming, meaning the first letter of the name needs to be lowercase, and then the first letter of any remaining words should be capitalized. Avoid naming a public property with an underscore at the beginning. Depending on the coding standard, this may visually indicate that it's actually private access only. When you're declaring a property, you can optionally initialize it with the default value. If you do, it must be an unchanging value. This means that it must be able to be evaluated upon compilation. So, things that change, like the current time, are not allowed.
Let's clean up all the test code, then build upon the first address class. public $streetName. This is a valid property name that follows best practices. public $subdivision_name = California. This is also a valid property name that includes an initialization. public $_city. This is a valid property name, but as described before, does not follow best practices by implying that it has a different variable scope.
public $time_updated = time. This is a valid property name, but as the value is determined at runtime, this is an illegal initialization, and will generate a parse error. public $-addressId. This is an invalid property name, as it starts with a dash, not an underscore. This will generate a parse error, expecting a T variable. public $city_copy = $_city. This is an invalid initialization, as it depends on a runtime evaluation.
So, it will also trigger a parse error. Let's clean up this example, and make it useful in a real world application, and add some documentation. For the time being, I'll use human readable data for properties with known domains, such as the city, subdivision, and country name. We'll start with basic class documentation. I will add the street address properties. public $street_address_1, public $street_address_2.
Following the address, let's add the structure for the name of the city. public $city_name. After the city, I'll add the name of the subdivision. This can be a state, territory, region, and so forth. public $subdivision_name. Most addresses have a postal code of some sort. public $postal_code. And finally, the name of the country. public $country_name.
Now that we have a class definition, let's save it before we move on to creating a method in the next video.
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.