Solved

know to go back to DocumentObject::getGroup

Posted on 2014-04-23
5
151 Views
Last Modified: 2014-04-28
<?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
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 250 total points
ID: 40019238
Through inheritance. The Spreadsheet class inherits Document's methods.
This includes the getGroup() method (which is overriden in the Document class).

HTH,
Dan
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40019814
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
 

Author Comment

by:rgb192
ID: 40021859
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40022403
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
 

Author Closing Comment

by:rgb192
ID: 40028773
thanks for examples about calling methods.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now