Page 1 of 1

PHP Classes (extends help)

Posted: Fri Jun 30, 2006 9:41 am
by zeen
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Comments on this please:

Code: Select all

//---------------------------------

class Core {
var $controller;
function Core() {
//set up controller
$this->controller = new Controller;
}
}

//---------------------------------

class Controller extends Core {
var $page;
function Controller {
//set up mypage class
$this->page = new MyPage;
}
}

//---------------------------------

class MyPage extends Controller {
function MyPage() {
//I want to call functions from controller and core
//should I pass the Core object to Controller then Core and Controller to MyPage
//or use parent::
}
}

//---------------------------------
Questions:

* I want to access Core and Controller functions from MyPage, should I use parent:: or pass the object references down to MyPage? Passing the objects will get messy because I may have more 'core' objects to pass at a later date.

* If i want to access a public variable in Core do I have to create a function for this? e.g. getSetting();

* If i load another module though Core, so I have Core, Controller and say UrlParser how should I access UrlParser though Controller?

--

I'm still pretty new to OO and I want to do it correctly but finding it hard to find good tutorials/help on these kind of issues so any help would be excellent!

If anyone can point me in the right direction for proper PHP OO design that would be wicked too.

Thanks!

zeen


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Jun 30, 2006 10:03 am
by jamiel
Controller should construct core (parent::__construct() ) and MyPage should construct Controller. You can just access the public variable with $this . eg. $this->controller . URLParser should also extend Controller if it has nothing to do with MyPage . Controller shouldn't instantiate MyPage.

work backwards

Posted: Fri Jun 30, 2006 10:17 am
by zeen
I always imagined it was from the top code to the bottom code, so i've done it the wrong way around?

MyPage sets up Controller
Controller sets up Core.

Does Controller extends from MyPage?

I think I need to get a book about this because MyPage class could have been named anything like ProductsPage, CompanyPage (a class for each section of the site) and i dont really want to be constructing within these classes each time as i'll be repeating code for each section.

I wanted it to work something like this:

Core - sets up controller and settings for page
Controller - gets the section been called and sets up the relevent page class depending on the URL
MyPage/ProductsPage/CompanyPage - to build the html and send back to 'core' to render

I'll have to rethink and look at some examples.

Thanks for your help, anymore help would be appreciated.

Re: PHP Classes (extends help)

Posted: Fri Jun 30, 2006 10:41 am
by Christopher
How about just:

Code: Select all

//---------------------------------

class Controller {
    var $controllerVar;
    function controllerMethod() {}
}

//---------------------------------

class Page extends Controller {
    var $pageVar;
    function pageMethod() {}
}

//---------------------------------

class MyPage extends Page {
    function MyPage() {
        $this->controllerMethod();
        $this->pageMethod();
        $this->controllerVar = 'foo';
        $this->pageVar = 'bar';
    }
}

//---------------------------------