Free Computer Books

   »    Free Computer Books on Computer Programming, Web Development & Designing

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Computer Books »

3.6 ACCESSING METHODS AND PROPERTIES USING THE $this
VARIABLE

During the execution of an object’s method, a special variable called $this is
automatically defined, which denotes a reference to the object itself. By using
this variable and the -> notation, the object’s methods and properties can be
further referenced. For example, you can access the $name property by using
$this->name (note that you don’t use a $ before the name of the property). An
object’s methods can be accessed in the same way; for example, from inside one
of person’s methods, you could call getName() by writing $this->getName().


• • •
 

3.5 DESTRUCTORS
Destructor functions
are the opposite of constructors. They are called when
the object is being destroyed (for example, when there are no more references
to the object). As PHP makes sure all resources are freed at the end of each
request, the importance of destructors is limited. However, they can still be
useful for performing certain actions, such as flushing a resource or logging
information on object destruction. (more…)


• • •
 

3.4 THE new KEYWORD AND CONSTRUCTORS
Instances of classes are created using the new keyword. In the previous example,
we created a new instance of the Person class using $judy = new Person();. What
happens during the new call is that a new object is allocated with its own copies
of the properties defined in the class you requested, and then the constructor of
the object is called in case one was defined. (more…)


• • •
 

3.3 DECLARING A CLASS
You might have noticed from the previous example that declaring a class (an
object template) is simple. You use the class keyword, give the class a name,
and list all the methods and properties an instance of this class should have:
class MyClass {
… // List of methods

… // List of properties

}
You may have noticed that, in front of the declaration of the $name prop-
erty, we used the private keyword. We explain this keyword in detail later, but
it basically means that only methods in this class can access $name. It forces
anyone wanting to get/set this property to use the getName() and setName()
methods, which represent the class’s interface for use by other objects or
source code.


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com