This script prints in .NET Generator Code 3 of 9 in .NET This script prints

This script prints using none toincoporate none with web,windows application Microsoft Excel An object of type MyClass is being destroyed In this exam ple, when $obj = NULL; is reached, the only handle to the object is destroyed, and therefore the destructor is called, and the object itself is destroyed. Even without the last line, the destructor would be called, but it would be at the end of the request during the execution engine s shutdown. Tip: The exact point in time of the destructor being called is not guaranteed by PHP, and it might be a few statements after the last reference to the object has been released.

Thus, be aware not to write your application in a way where this could hurt you.. 3.6 ACCESSING METHODS AND PROPERTIES USING THE $this VARIABLE During the e xecution of an object s method, a special variable called $this is automatically de ned, 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().. Gutmans_ch03 Page 60 Thursday, September 23, 2004 2:38 PM PHP 5 00 Language Chap. 3 3.6.1 public none none , protected, and private Properties A key paradigm in OOP is encapsulation and access protection of object properties (also referred to as member variables).

Most common OO languages have three main access restriction keywords: public, protected, and private. When de ning a class member in the class de nition, the developer needs to specify one of these three access modi ers before declaring the member itself. In case you are familiar with PHP 3 or 4 s object model, all class members were de ned with the var keyword, which is equivalent to public in PHP 5.

var has been kept for backward compatibility, but it is deprecated, thus, you are encouraged to convert your scripts to the new keywords:. class MyClas s { public $publicMember = "Public member"; protected $protectedMember = "Protected member"; private $privateMember = "Private member"; function myMethod(){ // ...

} } $obj = new MyClass();. This example none none will be built upon to demonstrate the use of these access modi ers. First, the more boring de nitions of each access modi er:. public. Public members can be accessed both from outside an object by using $obj-& none for none gt;publicMember and by accessing it from inside the myMethod method via the special $this variable (for example, $this->publicMember). If another class inherits a public member, the same rules apply, and it can be accessed both from outside the derived class s objects and from within its methods. protected.

Protected members can be accessed only from within an object s method for example, $this->protectedMember. If another class inherits a protected member, the same rules apply, and it can be accessed from within the derived object s methods via the special $this variable. private.

Private members are similar to protected members because they can be accessed only from within an object s method. However, they are also inaccessible from a derived object s methods. Because private properties aren t visible from inheriting classes, two related classes may declare the same private properties.

Each class will see its own private copy, which are unrelated.. Gutmans_ch03 Page 61 Thursday, September 23, 2004 2:38 PM 3.6 Accessing Methods and Properties Using the $this Variable Usually, you would use public for members you want to be accessible from outside the object s scope (i.e., its methods), and private for members who are internal to the object s logic.

Use protected for members who are internal to the object s logic, but where it might make sense for inheriting classes to override them:. class MyDbCo none none nnectionClass { public $queryResult; protected $dbHostname = "localhost"; private $connectionHandle; // ...

} class MyFooDotComDbConnectionClass extends MyDbConnectionClass { protected $dbHostname = ""; }. This incompl ete example shows typical use of each of the three access modi ers. This class manages a database connection including queries made to the database:. The connec none none tion handle to the database is held in a private member,. because it i s used by the class s internal logic and shouldn t be accessible to the user of this class. In this example, the database hostname isn t exposed to the user of the class MyDbConnectionClass. To override it, the developer may inherit from the initial class and change the value.

The query result itself should be accessible to the developer and has, therefore, been declared as public. Note that access modi ers are designed so that classes (or more speci cally, their interfaces to the outer world) always keep an is-a relationship during inheritance. Therefore, if a parent declares a member as public, the inheriting child must also declare it as public.

Otherwise, the child would not have an is-a relationship with the parent, which means that anything you can do with the parent can also be done with the child. 3.6.

2 public, protected, and private Methods Access modi ers may also be used in conjunction with object methods, and the rules are the same:. public met none for none hods can be called from any scope. protected methods can only be called from within one of its class methods.
Copyright © . All rights reserved.