Page 1 of 2

[SOLVED]OOP: Mother, may I know your name?

Posted: Tue Aug 30, 2005 9:59 pm
by harrisonad
The story is about 'classy' James(not the real name) detained inside his 'classy' mother, Elena (also not the real name). One day at work, James needs to know his mother's name in order for him to complete his job.

Code: Select all

class Mother
{
    var $name;
    var $child;
	
    function setChild($child_obj){
        $this->child = $child_obj;
    }
    function getChild(){
        return $this->child;
    }
}

class Son
{
    function getMothersName(){
        return "Can't tell.";
    }
}

$james = new Son();
$elena = new Mother();
$elena->setChild($james);
$son = $elena->getChild();
echo $son->getMothersName();
Can you help james?

Posted: Tue Aug 30, 2005 10:31 pm
by feyd

Posted: Tue Aug 30, 2005 10:39 pm
by harrisonad
I thought the keyword 'parent' works when using 'extend' such as

Code: Select all

class Son extend Mother
But if you look at my post, the Son object is only a property of Mother obj.

Posted: Tue Aug 30, 2005 10:47 pm
by feyd
yes, sorry.. technically, they should be the same class though. The child would have to be notified of it's "parent" object. Typically I have an attach parent method.

Posted: Wed Aug 31, 2005 3:18 am
by patrikG
Simply because class Mother and Son are in one file doesn't mean they are related. It's like putting a small boy and a woman in one room and saying they must be related because they are in one room. Unless you Son inherits from Mother (as you said: use extends), there is no relationship and "parent" won't work.

What you're trying to do is called aggregation. Aggregation means one object temporarily takes control over another - bit like an occupation. However, the way you're doing it would be something which could be called reverse aggregation - which doesn't work. An aggregated object (James) does not inherit anything from its aggregator (Mom) and cannot access properties or methods due to this aggregation, even though you're trying to cement the relationship by declaring "$son = $elena->getChild();".
You are telling Mom that her new son is James, but don't tell James about it. Thus, James doesn't know who his mom his. Solution: James needs to talk to Mom and ask for her name. That can be done with inheritance, i.e.

Code: Select all

class Son extends Mother
(where child-objects have access to their parents properties and classes) or aggregation: Son adopts Mom:

Code: Select all

class Mother{
    var $name;

    function Mother($name){
        $this->name=$name;
    }
}

class Son{
    var $mom;
    function Son($mom){
        $this->mom=$mom;
    }
    function getMothersName(){
        return $this->mom->name;
    }
}

$elena =&new Mother("Elena");
$james =&new Son(&$elena);
echo'<H3>James Mum is called: ';print_r($james->getMothersName());echo'</H3>';
echo'<H3>_$elena</H3><pre>';print_r($elena);echo'</pre>';
echo'<H3>_$son</H3><pre>';print_r($james);echo'</pre>';

Posted: Wed Aug 31, 2005 3:29 am
by harrisonad
Cool! 8) The aggregation thing is explained very well. Thank you, sir.

As a real senario, I tried that in making a page class with security configuration as one of its properties

Code: Select all

// class declarations
class Page
{
    // ...
    var $url;
    // ...
    var $security_config;

    // ....     
    function setupSecurityConfig($security_config){
        $this->security_config = $security_config;
    }
   
    function getSecurityConfig(){
        return $this->security_config;
    }
}

class SecurityConfig
{
    // ...
    function BuildLoginPage(){
        $page_url = "don't know" // <-- need this one 
        $str = "
        <form method=post action='$page_url'>";
        // ...
        return $str;
    }

}

// script
$page = new Page();
$page->setupSecurityConfig(new SecurityConfig());
$security = $page->getSecurityConfig();
echo $security->BuildLoginPage();
BTW, they are in separate files.

Posted: Wed Aug 31, 2005 4:05 am
by patrikG

Code: Select all

function BuildLoginPage(){
        $page_url = "don't know" // <-- need this one
        $str = "
        <form method=post action='$page_url'>";
        // ...
        return $str;
    }
Hrmpf... html in a class is not really good at all. See A Quick Intro to N-Tier (google cache, phppatterns was down for some reason). The reason why it's bad is because you're mixing up application layers which complicates things.

Posted: Wed Aug 31, 2005 4:42 am
by Chris Corbyn
patrikG wrote:

Code: Select all

function BuildLoginPage(){
        $page_url = "don't know" // <-- need this one
        $str = "
        <form method=post action='$page_url'>";
        // ...
        return $str;
    }
Hrmpf... html in a class is not really good at all. See A Quick Intro to N-Tier (google cache, phppatterns was down for some reason). The reason why it's bad is because you're mixing up application layers which complicates things.
On that note... I try to keep as much separation of presentation from logic as I can but I still can't even get close to 100%.

Anybody know any good advanced books on the topic?

Posted: Wed Aug 31, 2005 5:17 am
by harrisonad
patrikG wrote:html in a class is not really good at all.
What do you mean? I saw some classes for building HTML strings.

Posted: Wed Aug 31, 2005 5:31 am
by patrikG
Do have a look at the N-Tier article I've linked to. Think of websites as applications and read this php|arch "An introduction to the Model-View-Controller Pattern"

Posted: Wed Aug 31, 2005 9:57 am
by patrikG
Just discovered that there is actually a song about MVC: http://home.in.tum.de/~pittenau/ModelViewController.mp3 :lol:

Posted: Wed Aug 31, 2005 10:00 am
by feyd
patrikG wrote:Just discovered that there is actually a song about MVC: http://home.in.tum.de/~pittenau/ModelViewController.mp3 :lol:
that's kinda sad; definitely disturbing. :lol:

Posted: Wed Aug 31, 2005 11:58 am
by McGruff
harrisonad wrote:What do you mean? I saw some classes for building HTML strings.
Good objects are cohesive that is to say they round up some properties and methods relating to a specific task. Good OOP design is about identifying different responsibilities and encapsulating these.

The conceptual starting point is the standard presentation / domain / data access layering. The presentation layer is the UI: it receives input, decides what to do with it, manipulates the domain and creates a browser page. In MVC the presentation layer is further split into controller and view but the really important separation is between presentation/domain. The domain is the all the business logic: things like calculating the interest due on an account, identifying user permissions, and so on. Domain objects add some kind of meaning to raw data. Data access is the code which gets raw data from persistent storage - a database, csv files, whatever.

That's the broad brush: each layer will break down further until you finally get to individual responsibilities - those will be your classes. Formatting (html) is firmly in the presentation layer. You can mix html and php but only if the code is carrying out presentation logic. Perhaps some conditionals such as:

Code: Select all

if($account->isOverdrawn()) {
    echo <span style="color: red;">$account->getBalance()</span>;
} else {
    echo $account->getBalance();
}
"Presentation logic" is a slightly confusing term: it simply means "formatting logic" rather than all aspects of the presentation layer.

Note that "is overdrawn" is a domain concept. A boolean flag would be set by a domain object. The view can decide how it wants to present this - perhaps it would be formatted differently on different pages or, if you wanted a CLI version, you wouldn't format with html at all. The fact that you've clearly separated domain and presentation allows you create different kinds of output without duplicating code. The domain remains the same.

I'm not a fan of classes which output tables and the like: I prefer just to define sets of data in the main body of the code and then do all the formatting (ie add the html) in the template.

Posted: Sat Sep 03, 2005 2:06 am
by Mastermind
Ano ibig mo sabihin Pare

Posted: Sat Sep 03, 2005 3:18 am
by n00b Saibot
Mastermind wrote:Ano ibig mo sabihin Pare
8O :?