Page 1 of 2
avoiding re-declaring withen classes
Posted: Sat Dec 18, 2004 7:18 pm
by John Cartwright
okay let me explain my situation as best as possible. For the past few months I've developped a strong sense of OOP style coding but have some to a sort of small problem that I believe should be easily solved.
Lets say I have multiple classes, organized in seperate files in the classes folder. But lets say I have a class to handle all my "news" related business, such as adding, editing, etc. And then I have my template engine which handles obvious thing.
Now lets say in my news class I need to output something to be parsed, is there a proper way to use my templating class without have to re-declare it?
I have tried declaring all my classes on my index page, which then includes the request pages, but it seems the declaring has to be done on each individual page that I use the particular class on. I hope this makes sense.
let me make it visual for those of you that don'tn like reading
Code: Select all
<?php
class news {
function add {}
function shownews{
$template = new template();
$template->var(array('header' => 'blah' ));
}
}
?>
I have read about
extends but can't quite apply it. Please help

Posted: Sat Dec 18, 2004 8:49 pm
by timvw
each class definition goes in a separate file, prefer to call it foo.class.php or class.foo.php
then in each script (or class) where i need it:
Code: Select all
<?php
require_once(CLASS_PATH . 'foo.class.php');
class Bar {
}
?>
Re: avoiding re-declaring withen classes
Posted: Sat Dec 18, 2004 8:57 pm
by andre_c
Phenom wrote:...
Now lets say in my news class I need to output something to be parsed, is there a proper way to use my templating class without have to re-declare it?
...
What exactly do you mean by declare it?

It should probably be declared only once on the class file.
Then extended only to create a more specific class, or give it more features.
If you mean instantiate, then it depends on you system and how it's designed...
I'm no expert so whatever i say is just my current opinion

Re: avoiding re-declaring withen classes
Posted: Sat Dec 18, 2004 9:42 pm
by neophyte
Phenom wrote:
I have tried declaring all my classes on my index page, which then includes the request pages, but it seems the declaring has to be done on each individual page that I use the particular class on. I hope this makes sense.
let me make it visual for those of you that don'tn like reading
As far as I know you have to instantiate/redeclare objects on every page. Athough I'm working on a class that serializes (serialize()) the object into a session variable. Then on the second page I can still use the object with out instantiating/creating a new object (ala $object = new thingey).
Posted: Sat Dec 18, 2004 9:42 pm
by John Cartwright
okay... let me re-explain myself because I didn't do a very good job I guess
Code: Select all
<?php
include('classes/class.templating.php');
include('classes/class.news.php');
$template = new templating(); #have to declare the class before i use it
$template->svar ... #blah blah blah
?>
now looking at the newsclass
Code: Select all
<?php
class news {
function shownews{
$template = new template();
$template->var(array('header' => 'blah' ));}}
?>
Is there not any other way so I do not have to declare another class withen a class? For example, I wish I could have all the classes declared on the index page but it is forcing me to declare it on the page I'm using it on, including the seperate classes. Am I making sense?
Posted: Sat Dec 18, 2004 9:47 pm
by andre_c
then you mean instantiate,
you can do something like this
Code: Select all
<?php
class news {
var $template;
function shownews {
$template->var(array('header' => 'blah' ));
}
}
?>
now when you instantiate news make
Code: Select all
<?
$news = new news;
$news->template = &$template; //template object already instantiated
?>
if you're using php5, you don't need the ampersand
Posted: Sat Dec 18, 2004 9:49 pm
by John Cartwright
Oops, yea your right I didn't mean declare

Sorry about the confusion its been a long day. TY for words of wisdom
Posted: Sat Dec 18, 2004 9:58 pm
by John Cartwright
Fatal error: Call to a member function svar() on a non-object in C:\*\*\classes\class.news.php on line 104
Code: Select all
<?php
$template = new templating();
$db = new database();
$news = new news();
$news->template = &$template;
?>
Code: Select all
<?php
class news
{
var $template;
function displayform($currently,$title,$body)
{
$template->svar(array('NEWS_TITLE' => $title,
'NEWS_BODY' => $body,
'NEWS_CURRENTLY' => $currently
));
$template->loadfile('newsform');
}
}
?>
Posted: Sat Dec 18, 2004 10:03 pm
by andre_c
you need to do $this->template->svar ....
Posted: Sat Dec 18, 2004 10:07 pm
by John Cartwright
Undefined property: news::$ in C:\*\*\classes\class.news.php on line 104
Fatal error: Call to a member function svar() on a non-object in C:\*\*\classes\class.news.php on line 104
which is
Code: Select all
<?php
$this->$template->svar(array('NEWS_TITLE' => $title,
'NEWS_BODY' => $body,
'NEWS_CURRENTLY' => $currently
));
?>
im sorry for being so numb-minded, been a really long day
Posted: Sat Dec 18, 2004 10:08 pm
by andre_c
$this->template not $this->$template
Posted: Sat Dec 18, 2004 10:10 pm
by John Cartwright
someone please delete my life.... im signing off on that note
ty
Posted: Sat Dec 18, 2004 10:12 pm
by neophyte
andre_c, what's the ampersand do?

Posted: Sat Dec 18, 2004 10:15 pm
by andre_c
it makes it so the object is passed by reference.
in php4 objects are passed by value by default
in php5 they're passed by reference by default
Posted: Sat Dec 18, 2004 10:18 pm
by neophyte
I'm going to have to read up on that reference thing. Time for php.net. Thanks andre_c