| Home / Section index | www.icosaedro.it | ![]() |
Contents Introduction Basic features Packages Importing modules Importing packages Namespaces Constants Global variables Types Arrays Control structures Functions Type compatibility Typecasting Predefined constants Predefined superglobals Classes Recursive declarations Errors Exceptions PHPLint's Std. Library Autoloading classes Definite assignment analysis PHP4 - Classes PHP4 - Type conversion operators Documentator Usage How To... Reserved keywords To-do list Memorandum License References Syntax |
Contents of this chapter New attributes for class items PHP4 - ClassesPHP4 classes declare properties and methods. PHPLint extends the syntax of the PHP 4 classes adding the visibility attributes (public, protected and private), abstract, final and static. Since these attributes are not allowed by PHP 4, they can be specified either through the PHPLint meta-code or through a DocBlock comment. In the examples that follow we will use the PHPLint meta-code because it is shorter. PHPLint uses these informations in several ways:
New attributes for class itemsThe new visibility attributes are:
Any non-static item declared inside a class is by default tied to an
object and can be accessed through the "dynamic" dereferencing operator
" The final attribute marks methods that cannot be overridden. A private final item is allowed, although it does not make much sense, since if it is private isn't visible from outside the class. Class constants
PHP 4 does not provide support for class constants.
Constants can be declared using the PropertiesProperties are variables automatically instantiated for every new object of the class. Every object has its own set of properties.
All the properties of the class must be declared explicitly (PHPLint
restriction). At run-time you cannot assign new properties to an existing
object, so that A property can be declared using a syntax similar to this one:
Note that several properties can be declaredi in a single statement, all having the same type and visibility attributes. The visibility attribute is optional, and the default is public. The type is optional. It is simpler and often shorter to indicate a proper initial value. Properties cannot be final nor static. public and protected properties cannot be overriden nor re-defined in extended classes (PHPLint restriction). private properties cannot be re-defined (PHPLint restriction due to the PHP 4 lack of actual support of the private visibility attribute). Properties can be accessed in several ways:
Lets see how a property can be declared through some examples:
A public property of default type mixed and undefined initial value. PHPLint raises a warning on this declaration, since the default value of this property once the object will be created is NULL, a value that might not be suitable for the intended usage of the variable.
Three public properties of the type int, string and resource respectively. Note that the NULL value requires a formal type-cast, as we already explained.
A private property of type array[int]string, initially empty, and that cannot be used outside its class. MethodsMethods can have one of the visibility attributes private or protected or public. The default attribute is public. private methods are accessible only inside the code of the class itself, and are not visible outside. private methods cannot be overridden in the child classes (PHP4 limitation). protected methods are accessible only from the class itself and its child classes. public methods are always accessible and can be overridden. Methods can have the static attribute. static methods cannot use the special variable $this. static methods can be accessed only through the "::" operator. Non-static methods (the default) can be accessed only through the "->" operator. Methods can have the final attribute. final methods cannot be overridden. private methods cannot be final. The general syntax of methods is the same we already seen for the regular functions (see chapter Functions), apart the attributes. Lets see how a method can be declared through some examples:
A public, non-static, method of default type undefined. PHPLint
can guess the returned type from the
A protected, non-static, method that returns an int number. /*. public final array[int]string .*/
function getNames(/*. string .*/ $substr)
{
$res = /*. (array[int]string) .*/ array();
foreach($this->names as $v){
if( strpos($v, $substr) !== FALSE ){
$res[] = $v;
}
}
return $res;
}
A public, final, method that returns an array of strings. Methods can be accessed in several ways, depending on the context and on their attributes:
PHPLint will raise an error message if a static method does use the
special variable ExampleHere is a complete example of an (unuseful) class in PHP 4:
class A {
/*. public .*/ var
$prompt = "The current value is ",
$counter = 0;
/*. private .*/ var
$internal_counter = 0,
$list_of_names = /*.(array[int]string).*/ array();
function A(/*. int .*/ $n)
{
$this->counter = $n;
}
function get()
{
return $this->prompt . $this->counter;
}
/*. static string .*/
function getParam(/*. string .*/ $name)
{
if( isset( $_REQUEST[$name] ) )
return (string) $_REQUEST[$name];
else
return NULL;
}
}
$title = A::getParam("TITLE");
$obj = new A(789);
echo $obj->get(); # Output: "The current value is 789"
Overriding propertiesProperties cannot be overridden (PHPLint restriction). Overriding methodsA class B is said to be a subclass of the class A if the class B extends the class A or if the class B extends a class X that is a subclass of A (note the recursive definition). Normally, the names of the methods of a subclass differ from any other method of its parent class. However, the subclass B can override the method A::a() of its parent class defining the overriding method B::a(). The basic rule of the polymorphism in OOP is that overridden methods must be usable exactly as the original ones. For example, if A::a() is expected to return a string, the overriding method B::a() must return a string; if A::a() requires two mandatory arguments, also B::a() must be callable with two arguments. PHPLint checks accurately every overridden method: both the signature and the attributes are compared and possible incompatibilities are detected. Lets start defining what a signature is. The signature of a method is given by
For example, the signature of the method
is given by its return type (string), its mandatory arguments (one of type string), its optional arguments (one of type int) and its variable number of optional arguments (args). To be concise:
The attributes and the signature of the overriding method B::a() are subject to these rules (PHPLint restrictions):
In this example, the subclass B overrides all the methods of its parent class A. The body of all the methods is left empty, since it does not matter in our discussion. Note that B::g() adds a default argument $y, and B::h() adds optional arguments and raises its visibility from protected to public.
class A {
/*. void .*/ function f(){}
/*. int .*/ function g(/*. int .*/ $i){}
/*. protected void .*/ function h(/*. int .*/ $x){}
}
class B extends A {
/*. void .*/ function f(){}
/*. int .*/ function g(/*. int .*/ $x, /*. int .*/ $y = 0){}
/*. public void .*/ function h(/*. int .*/ $x /*., args .*/){}
}
Special methodsA method whose name is the same as the class is assumed to be the constructor. The constructor is called implicitly by the new operator. A class constructor can be called explicitly only inside the constructor of an extended class and it cannot be called explicitly elsewhere (PHPLint restriction).
The signature of the special methods /*. public array[int]string .*/ function __sleep(); /*. public void .*/ function __wakeup(); An error is raised if the method name begins with two underscore characters, since those names are reserver for future extensions of the language. Final classesA final class is a class that cannon be extended anymore. Since the final attribute is an extension to the PHP 4 language, it can be indicated either through PHPLint meta-code
The reasons why a class should be made "non-extensible" go beyond the aims of this reference manual. Take a good book about OOP if you are interested to the subject. In the following examples we will use the PHPLint meta-code because it is shorter, although you can use a DocBlock instead with exactly the same meaning. Abstract classesAn abstract class is a class with the abstract attribute in its declaration. The abstract attribute is an extension to the PHP 4 language, so it must be indicated either through meta-code
or unsing the Abstract classes can contain abstract methods, i.e. methods whose body is left empty. Abstract methods must have the abstract attribute:
An abstract class can have also non-abstracti (aka "concrete") methods and properties. The basic properties of abstract classes are:
This example should be self-explanatory. An abstract class provides the interface to a generic container of strings; every string has a name and its value can be written and read with the abstract methods set() and get():
Since this class is abstract it cannot be used directly to instantiate objects,
but it must be implemented in some concrete class. For example, the following
example shows two concrete classes that implements
The second implementation,
The power of abstract classes can be seen in the code below, where the
function can handle a container without knowing anything about its concrete
implementation. The object $container has only formally the type
|
| Umberto Salsi | Contact | Site map | Home / Section index |