Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 2 ■ STATIC VARIABLES, MEMBERS, AND METHODS^17

Listing 2-7. Invoking a Static Method

class MyObject {

static function myMethod() {
//Do something useful
}

}

MyObject::myMethod();

PHP 6 introduces a few new changes to the operation of static methods. First, you may no
longer use the :: operator to access methods that are not marked static. Correctly marking
your methods as static or non-static is now much more important than it was in previous
versions of PHP. Additionally, the static:: scope can also be applied to static methods. This
allows you to statically call a method of an inheriting class from within a parent class, as shown
in Listing 2-8.

Listing 2-8. The static Scope in PHP 6

class MyObject {

static function myMethod() {
static::myOtherMethod();
}

static function myOtherMethod() {
echo 'Called from MyObject';
}

}

class MyExtendedObject extends MyObject {

static function myOtherMethod() {
echo 'Called from MyExtendedObject';
}

}

MyExtendedObject::myMethod();

McArthur_819-9C02.fm Page 17 Friday, February 1, 2008 10:23 AM

Free download pdf