• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

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
0
rgb192
Asked:
rgb192
  • 2
  • 2
2 Solutions
 
Dan CraciunIT ConsultantCommented:
Through inheritance. The Spreadsheet class inherits Document's methods.
This includes the getGroup() method (which is overriden in the Document class).

HTH,
Dan
0
 
Ray PaseurCommented:
For better or worse, computer programming is a "precise science" requiring that we use the right names for variables and functions.No such method
0
 
rgb192Author Commented:
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?
0
 
Ray PaseurCommented:
Late Static Binding is a bit different from Inheritance.  If you read these two man pages carefully and find yourself wondering about how a programmer can keep track of what software is being executed, you're not alone.  Mercifully, PHP did not introduce multiple inheritance!

I have a shovel and a rake.  If I really try, I can gather leaves with the shovel and dig a hole with the rake.  But doing either would be a "run-time error."  That is kind of how I think of some of the more arcane programming constructs.  The simpler and more obvious I can make my code, the more likely I will finish the task quickly and accurately.
0
 
rgb192Author Commented:
thanks for examples about calling methods.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now