phpAspect Logo

Chapter 8. Inter-type declarations

Aspects can declare attributes, methods, constants and constructors that are owned by other types. These are called inter-type declarations.

Example 8.1: Attribute introduction

<?php
    //Declare $pearLog and $debug attributes in all types prefixed by Bo.
    private Bo*::$pearLog, Bo*::$debug=false;
    //Declare $instance attribute in Foo and Bar types.
    private static Foo,Bar::$instance = null;
?>


Example 8.2: Constant introduction

<?php
    //Declare PEAR_LOG_DEBUG and PEAR_LOG_ERR constants in class Log. 
    Log::PEAR_LOG_DEBUG=7, Log::PEAR_LOG_ERR=3;
?>


Example 8.3: Method introduction

<?php
    //Declare setLog method in all types prefixed by Bo.
    public function Bo*::setLog(Log $log){
        if($this->debug){
            $log->setMask(Log::PEAR_LOG_DEBUG);
        }else{
            $log->setMask(Log::PEAR_LOG_ERROR);
        }
        $this->pearLog = $log;
    }
?>