Tag Archives: php

Static variable inheritance in PHP

In version 5.1 of PHP, classes can override static variables. But there are a few kinks.

To start off, here’s the code I used to make this work:

ParentClass.php

class ParentClass {
    static $something = “I’m in the parent class”;

    static function getSomething() {
        return self::$something;
    }
}

Child1.php

class Child1 extends ParentClass {
}

Child2.php

class Child2 extends ParentClass {
    static $something = “I’m in the child class”;
}

Child3.php

class Child3 extends ParentClass {
    static $something = “I’m in the child class”;

    static function getSomething() {
        return self::$something;
    }
}

test.php

require_once(‘ParentClass.php’);
require_once(‘Child1.php’);
require_once(‘Child2.php’);

echo “Parent class: “;
echo ParentClass::$something;

echo “<br />Child1 class (no overriding): “;
echo Child1::$something;

echo “<br />Child2 class (overrides static var): “;
echo Child2::$something;

echo “<br />Child3 class (overrides static var): “;
echo Child3::$something;

echo “<hr /><br />Parent class: “;
echo ParentClass::getSomething();

echo “<br />Using the method (Child1): “;
echo Child1::getSomething();

echo “<br />Using the method (Child2): “;
echo Child2::getSomething();

echo “<br />Using the method (Child3): “;
echo Child3::getSomething();

Now here’s the output when you run it:

Parent class: I’m in the parent class
Child1 class (no overriding): I’m in the parent class
Child2 class (overrides static var): I’m in the child class
Child3 class (overrides static var): I’m in the child class


Parent class: I’m in the parent class
Using the method (Child1): I’m in the parent class
Using the method (Child2): I’m in the parent class
Using the method (Child3): I’m in the child class

Let’s analyze what’s going on here. The ParentClass implements a static variable called $something and assigns it a value. Child1 doesn’t override that but simply inherits it. Child2 and Child3 both override it to give it a different value. When we retrieve the value of that static variable directly from each class, we can see that it worked.

The tricky behavior is in the method. The ParentClass implements the static method that outputs $something. We call that method respectively from each class. What you’d expect is that it would output the value of the static variable as far down the inheritance line as the class from which it was called. But from the output it’s apparent that the value of self::$something depends only on the class where the method was actually implemented, not the class from which it was called.

Sending email through Gmail using PHP

Sending email is often a necessary feature in web applications. Here’s how you can send email from PHP by using SMTP through Gmail.

First, you’re going to need two PEAR packages: Mail and Net_SMTP. Make sure the directory where these files are found is listed in the include_path directive in your php.ini file.

This is based largely on a code snippet from email.about.com. Modify according to your needs. Entries in bold are ones you will definitely need to change.

<?php

require_once(‘Mail.php’);

$from = ‘Sender <sender@gmail.com>‘;
$to = ‘Receiver <receiver@something.com>‘;
$subject = ‘Sent from PHP on my machine‘;
$body = ‘This is a message I sent from PHP using the ‘
. ‘PEAR Mail package and SMTP through Gmail. Enjoy!
‘;

$host = ‘smtp.gmail.com’;
$port = 587; //According to Google you need to use 465 or 587
$username = ‘sender‘;
$password = ‘your_password‘;

$headers = array(‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array(
‘host’ => $host,
‘port’ => $port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo “Message sent successfully!”;
}
echo “\n”;

?>