Solved

know to go back to DocumentObject::getGroup

Posted on 2014-04-23
5
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 35

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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

724 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