Creating and Using Objects


To create an instance of an object from a class, you use the new keyword in PHP, as follows:

 $myObject = new myClass; 

In this example, myClass is the name of a class that must be defined in the scriptusually in an include fileand $myObject becomes a myClass object.

Multiple Objects You can use the same class many times in the same script by simply creating new instances from that class but with new object names.


Methods and Properties

The methods and properties defined in myClass can be referenced for $myObject. The following are generic examples:

 $myObject->myValue = "555-1234"; $myObject->myMethod(); 

The arrow symbol (->)made up of a hyphen and greater-than symbolindicates a method or property of the given object. To reference the current object within the class definition, you use the special name $this.

The following example defines myClass with a method that references one of the object properties:

 class myClass {   var $myValue = "Jelly";   function myMethod() {     echo "myValue is " . $this->myValue . "<br>";   } } $myObject = new myClass; $myObject->myMethod(); $myObject->myValue = "Custard"; $myObject->myMethod(); 

This example makes two separate calls to myMethod. The first time it displays the default value of myValue; an assignment within the class specifies a default value for a property. The second call comes after that property has had a new value assigned. The class uses $this to reference its own property and does not care, or even know, that in the script its name is $myObject.

If the class includes a special method known as a constructor, arguments can be supplied in parentheses when an object is created, and those values are later passed to the constructor function. This is usually done to initialize a set of properties for each object instance, and it looks similar to the following:

 $myObject = new myClass($var1, $var2); 

Using a Third-Party Class

The best way to learn how to work with classes is to use one. Let's take a look at a popular third-party class written by Manuel Lemos, which provides a comprehensive way to validate email addresses. You can download this class from www.phpclasses.org/browse/file/28.html and save the file locally as email_validation.php.

Manuel's class validates an email address not only by checking that its format is correct but also by performing a domain name lookup to ensure that it can be delivered. It even connects to the remote mail server to make sure the given mailbox actually exists.

DomainLookups If you are following this example on a Windows-based web server, you need to download an additional file, getmxrr.php, to add a suitable domain name lookup function to PHP. You can download this file from www.phpclasses.org/browse/file/2080.html.


The email_validation.php script defines a class called email_ validation_class, so you first need to create a new instance of a validator object called $validator, as follows:

 $validator = new email_validation_class; 

You can set a number of properties for your new class. Some are required in order for the class to work properly, and others allow you to change the default behavior.

Each object instance requires you to set the properties that contain the mailbox and domain parts of a real email address, which is the address that will be given to the remote mail server when checking a mailbox. There are no default values for these properties; they always have to be set as follows:

 $validator->localuser = "chris"; $validator->localhost = "lightwood.net";  

The optional timeout property defines how many seconds to wait when connected to a remote mail server before giving up. Setting the debug property causes the text of the communication with the remote server to be displayed onscreen. You never need to do this, though, unless you are interested in what is going on. The following statements define a timeout of 10 seconds and turn on debug output:

 $validator->timeout = 10; $validator->debug = TRUE; 

The full list of adjustable properties for a validator object is shown in Table 10.1.

Table 10.1. Properties of an email_validation_class Object

Property

Description

timeout

Indicates the number of seconds before timing out when connecting to a destination mail server

data_timeout

Indicates the number of seconds before timing out while data is exchanged with the mail server; if zero, takes the value of timeout

localuser

Indicates the user part of the email address of the sending user

localhost

Indicates the domain part of the email address of the sending user

debug

Indicates whether to output the text of the communication with the mail server

html_debug

Indicates whether the debug output should be formatted as an HTML page


The methods in email_validation_class are mostly private; you cannot call them directly, but the internal code is made up of a set of functions. If you examine email_validation.php, you will see function definitions, including Tokenize, GetLine, and VerifyResultLines, but none of these are useful outside the object itself.

The only public method in a validator object is named ValidateEmailBox, and when called, it initiates the email address validation of a string argument. The following example shows how ValidateEmailBox is called:

 $email = "chris@datasnake.co.uk"; if ($validator->ValidateEmailBox($email)) {   echo "$email is a valid email address"; } else {   echo "$email could not be validated"; } 

The return value from ValidateEmailBox indicates whether the validation check is successful. If you have turned on the debug attribute, you will also see output similar to the following, in addition to the output from the script:

 Resolving host name "mail.datasnake.co.uk"... Connecting to host address "217.158.68.125"... Connected. S 220 mail.datasnake.co.uk ESMTP C HELO lightwood.net S 250 mail.datasnake.co.uk C MAIL FROM: <chris@lightwood.net> S 250 ok C RCPT TO: <chris@datasnake.co.uk> S 250 ok C DATA S 354 go ahead This host states that the address is valid. Disconnected.  



    Sams Teach Yourself PHP in 10 Minutes
    Sams Teach Yourself PHP in 10 Minutes
    ISBN: 0672327627
    EAN: 2147483647
    Year: 2005
    Pages: 151
    Authors: Chris Newman

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net