Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

know to go back to DocumentObject::getGroup

<?php
abstract class DomainObject {
    private $group;
    public function __construct() {
        $this->group = static::getGroup();
    }

    public static function create() {
        return new static();        
    }

    static function getGroup() {
        return "from default"; 
    }
}

class User extends DomainObject {
}

class Document extends DomainObject {
    static function getGroup() {
        return "from document"; 
    }
}

class SpreadSheet extends Document {
}
echo'<pre>';
$userCreate=User::create();
print_r($userCreate);
$spreadSheetCreate=SpreadSheet::create();
print_r($spreadSheetCreate);
echo'</pre>';
?>

Open in new window


how does
$spreadSheetCreate=SpreadSheet::create();
know to go back to
DocumentObject::getGroup
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For better or worse, computer programming is a "precise science" requiring that we use the right names for variables and functions.User generated image
Avatar of rgb192

ASKER

sorry I meant
DomainObject::getGroup
not
DocumentObject::getGroup



go to different getGroup
$userCreate=User::create();
$spreadSheetCreate=SpreadSheet::create();

Through inheritance. The Spreadsheet class inherits Document's methods.
This includes the getGroup() method (which is overriden in the Document class).

Is this example of late static binding?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks for examples about calling methods.