Solved

know to go back to DocumentObject::getGroup

Posted on 2014-04-23
5
167 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 109

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 109

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 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