What is a class?
Video: What is a class?While primitives have fixed data structures, objects in comparison can seem loose, in that they can have any number of properties. To give structure to an object, I can use a class. A class is a blueprint that defines the structure of an object. Objects are created in the exact same way using these class blueprints. This way, I know that when a class is used as a blueprint for an object, the structure of each object won't be different. Each class modularizes program functionality by separating features with as little overlap as possible.
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
What is a class?
While primitives have fixed data structures, objects in comparison can seem loose, in that they can have any number of properties. To give structure to an object, I can use a class. A class is a blueprint that defines the structure of an object. Objects are created in the exact same way using these class blueprints. This way, I know that when a class is used as a blueprint for an object, the structure of each object won't be different. Each class modularizes program functionality by separating features with as little overlap as possible.
Classes have attributes, which are the data structure for properties. There is a tiny technical difference between attributes and properties, but it's so minor that even the official PHP documentation uses the terms kind of interchangeably, and prefers properties. So, we'll also use properties. Classes also define behaviors, which are implemented as methods. A method is a subroutine that works on an object. I like to think of methods like a function that's contained within a class. Objects are instances of a class, meaning each occurrence of an object of a particular class has all the properties and behaviors of that class.
The individual properties will be the same, but the value stored in the properties may differ. As an example, I'm going to define a class for an address. The class address has several properties like the city, and subdivision, and has the behavior of being able to look up a postal code given the values of those properties. Therefore, every instance of class address will have those properties and behavior. I'm going to apply this address class to the real world using two instances. The first instance can represent Phoenix, Arizona, and the second instance can represent Columbus, Ohio.
As each object is an instance of class address, I know that they should have at least a city and subdivision. And, I know that it has the behavior of postal code lookup. I can't anticipate the contents of the object properties, but I can anticipate their format, and how to access them. This seems like a lot of work. I know that I can write code that works perfectly fine without defining classes with properties and behaviors. So, why bother using object-oriented programming?
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.