Ten minutes To PHP Objects |
Written by Mike James | |||||
Thursday, 18 November 2021 | |||||
Page 1 of 4 PHP is a fully object-oriented language with lots of powerful features. This introductory guide looks at how PHP handles objects. Introduction to PHP
Contents
Many PHP programmers simply ignore its most powerful feature – it is fully object-oriented. When PHP was first introduced it was a simple scripting language but over time it has developed into a fully fledged language with all the usual modern conveniences. However, many PHP programmers still treat it as a sort of improved scripting language. Let’s take a look at how PHP handles objects. In this ten minute guide - which will probably in all honesty take you longer than ten minutes to read - it is assumed that you already have a PHP environment setup and working. If you want help with getting started see Ten Minutes to PHP. Class and ObjectsIf you have been using scripting in web pages you might think that object-oriented programming is overkill for what you mostly want to do. You can, of course, opt to use PHP without ever worrying about objects, but they aren’t difficult to work with and they provide a way of organising your code so that it is easier to reuse and modify. Even if you do try to avoid them you will probably encounter them sooner or later as part of some library or framework that you decide to use - so better to come to terms with them now. The following tutorial should give you a clear idea of what objects have to offer. Before we get started it is important to distinguish between a class and an object. A class is a specification for an object and once you have created a class you can use it to create as many objects, of that type, as you need.You can think of a class as a "cookie cutter" that you can use to stamp out as many cookies as you need in that particular shape. In this sense a class doesn’t actually do anything. It isn’t really executable PHP code (although this isn't quite true in a more advanced use of class code).. It basically just sits there waiting for someone to make use of it. For example, let’s suppose we need a calculator that can be used in any web page. So we might well proceed by defining a calculator class. <?php The class keyword is used to create a new class and the definition of the class goes between the curly brackets. In general an object can have properties and methods defined within the class. You can think of properties as being values or variables that relate to the object. For a calculator a property must be a variable like $answer which could be used to record the last answer. Properties behave like variables that belong to the class or object. They have to be declared using the public keyword if you want them to be accessible outside of the object. For example to give the calculator class an “answer” property, initialised to zero, you would write: class Calculator Now we have a class with a property we can try it out. To create an object from a class we use new the command: $mycalc=new Calculator; Whenever you have a class you can use the same sort of instruction to create an instance of the class. Each instance you create has all of the properties and functions that you define in the class. You can create as many instances of a class you require. Once you have created an instance you can access its properties using expressions like: $mycalc->answer This can be read “mycalc’s answer property”. Notice that it is the name of the instance or the object that you use and not the class. Properties can be used just like variables. For example: $mycalc->answer=10; Every instance of a class you create has its own properties and methods. For example, if you try: $mycalc1=new Calculator; then you will see 10 and 20 printed. This demonstrates that each instance is a complete and distinct incarnation of the class that you have defined. At this point you should be happy with the idea that the class defines the object in the sense that it determines what properties and methods an object created using it will have. To create an instance of an object you simply use $variable=new classname; and every time you create an instance the object has a completely new set of properties and methods that are independent of any other instance. The properties of an object behave just like variables but you have to refer to them using a "fully qualified" name: objectname->propertyname |
|||||
Last Updated ( Thursday, 18 November 2021 ) |