Now that you've got the concepts straight, let's take a look at the nitty-gritty of a class definition. Every class definition begins with the keyword "class", followed by a class name. You can give your class any name that strikes your fancy, so long as it doesn't collide with a reserved PHP word. A pair of curly braces encloses all class variables and functions, which are written as you would normally code them. In order to create a new instance of a class, you need to use the "new" keyword, and assign the newly-created object to a PHP variable. In English, the above would mean "create a new object of class Automobile and assign it to the variable $his". You can now access all the methods and properties of the class via this variable. Again, in English, would mean "assign the value Ferrari to the variable $make of this specific instance of the class Automobile", while would mean "execute the function accelerate() with parameter 180 of this specific instance of the class Automobile". Note the -> symbol, and the fact that the $ symbol is omitted when accessing variables of a specific class instance.
blog comments powered by Disqus |