corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » OOP PHP Classes and Sub-Classing


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author OOP PHP Classes and Sub-Classing
ed
Member

Registered: 10th Sep 03
User status: Offline
16th Feb 11 at 15:59   View User's Profile U2U Member Reply With Quote

I think I'm making a right hash of this, but there are a few people on here who do PHP right?

I've written a CMS that utilises and object called page, this loads basic stuff into the object as methods and properties of that class - these include stuff like page metadata that doesn't really change from page to page, but is loaded from either a config file or the database. I am trying to load a sub-class to extend this class with page specific information, I call this class application. This sub-class loads items such as the page title and the actual HTML that should be rendered on the page, this is all within a separate php file and the arrangement looks something like this:

code:
<?

//page.php

class page{

public function loader(){
//Do stuff
return path/to/applocation.php
}

//Do more stuff here

}

//applocation.php

class application extends page{

public $pageTitle='My OOP Website';

public function pageHTML(){
//Get the page from the database

return $pageHTML;
}

}

//index.php

require_once('page.php');

$page=new page;

require_once($page->loader());

//Then what do I do to extend the class 'page'

?>


So basically index.php loads the class page, it then loads the script with the application class but then I get stuck. What do I actually need to do to extend the page class, do I create a new instance of the application class which will be added to the current instance of page ($page)?

Sorry if it's a bit vague
Dom
Member

Registered: 13th Sep 03
User status: Offline
16th Feb 11 at 17:03   View User's Profile U2U Member Reply With Quote

Not much help, but OO is eugh!
ed
Member

Registered: 10th Sep 03
User status: Offline
16th Feb 11 at 18:23   View User's Profile U2U Member Reply With Quote

I like OOP, I think I've been doing too much Javascript and it's broked my head
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
16th Feb 11 at 18:46   View User's Profile U2U Member Reply With Quote

ed,
Why are you calling page in the index file? all of the attributes, functions from page are inside the application class so you should be calling that.
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
16th Feb 11 at 18:52   View User's Profile U2U Member Reply With Quote

//index.php

require_once('application.php');


$objApplication=new application;
$objApplication->loader();



If you want to add to the page class then thats fine and the application class will inherit these functions.
ed
Member

Registered: 10th Sep 03
User status: Offline
16th Feb 11 at 19:17   View User's Profile U2U Member Reply With Quote

There are more things in index.php than just the page class, like user authentication e.t.c.

So I call the sub-class before declaring the main class and then it would work?
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
16th Feb 11 at 19:52   View User's Profile U2U Member Reply With Quote

well if i was doing it, I would probably have an authentication class and make that instantiate in the constructor of the page class.
I dont think calling the subclass will work, I dont quite follow what you are trying to do.

i think the structure should go:
An Application has many pages so one to many.
So the application class shouldnt extend the class, instead should instantiate the page class inside itself.

Does that make sense?
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
16th Feb 11 at 20:05   View User's Profile U2U Member Reply With Quote

application class
{
$arrPages = array();

public function addPage()
{
$objNewPage =new page;
$this->Pages[] = $objNewPage;
}
}


//something like that, would probably work out better.

[Edited on 16-02-2011 by Reedy]
ed
Member

Registered: 10th Sep 03
User status: Offline
16th Feb 11 at 20:09   View User's Profile U2U Member Reply With Quote

Authentication is kept separate as a lot of stuff is done via AJAX. It's a little bit messy at the moment, but I'm learning it and tidying it all up

I get what you mean about the names, I've called the sub class an application in the sense that you can install a blog app or a media app (etc) into the page. Not sure if it makes sense to do it like that?
Reedy
Member

Registered: 11th Apr 04
Location: Hammersmith
User status: Offline
16th Feb 11 at 21:08   View User's Profile U2U Member Reply With Quote

if you have time to, i would suggest you use a strict framework like Kohana, its a modification of the mvc framework but it has a shed load of helper tools and its very structual making your code and files very tidy.

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
CS Run what you brung day.. nik General Chat 188 5746
2nd Jul 03 at 22:00
by corsaCsxi
 
http://cs.stx-hosting.com shawboy General Chat 4 134
26th Jun 03 at 21:22
by Red_Sxi
 
OK ICE heads. Help me out with this one..... SteveW Help Zone, Modification and ICE Advice 25 511
31st Jul 03 at 09:31
by Rob
 
Gold GSI? WATSON General Chat 47 1087
10th Oct 07 at 22:34
by Hamish
 
[XBOX:360] Forza 3 Doug Geek Day 159 3354
14th Jan 11 at 15:16
by Adam_B
 

Corsa Sport » Message Board » Off Day » Geek Day » OOP PHP Classes and Sub-Classing 29 database queries in 0.0132148 seconds