PHP Data Types
PHP Statements
Reuse Code in PHP Programs through Functions

How to Write a Class Statement

You write the PHP class statement to define the properties and methods for the class. The class is the template (pattern) for the object. The class statement has the following general format:

class className
{
 
    Add statements that define the properties
    Add all the methods
}

You can use any valid PHP identifier for the class name, except the name stdClass. PHP uses the name stdClass internally, so you can't use this name.

All the property settings and method definitions are enclosed in the opening and closing curly braces. If you want a class to be a subclass that inherits properties and methods, use a statement similar to the following:

class whiteRose extends Rose
{
    Add the property statements
    Add the methods
}

The object created from this class has access to all the properties and methods of both the whiteRose child class and the Rose class. The Rose class, however, doesn’t have access to properties or methods in the child class, whiteRose. Imagine, the child owns everything the parent owns, but the parent owns nothing of the child's. What an idea.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Convert mysqli Functions to mysql Functions
The Basics of Writing PHP Code
How to Use PHP Variables for HTML5 and CSS3 Programming
Troubleshooting a PHP Script
Building SQL Queries
Advertisement

Inside Dummies.com