avoiding re-declaring withen classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

avoiding re-declaring withen classes

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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  {
}
?>
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: avoiding re-declaring withen classes

Post 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 :wink:
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: avoiding re-declaring withen classes

Post 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).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

okay... let me re-explain myself because I didn't do a very good job I guess

Code: Select all

&lt;?php
include('classes/class.templating.php');
include('classes/class.news.php');

$template = new templating(); #have to declare the class before i use it
$template-&gt;svar ... #blah blah blah


?&gt;
now looking at the newsclass

Code: Select all

&lt;?php
class news { 

function shownews{

$template = new template();
$template-&gt;var(array('header' =&gt; 'blah' ));}}

?&gt;
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?
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

then you mean instantiate,
you can do something like this

Code: Select all

&lt;?php
class news {
   var $template;
  function shownews {
    $template-&gt;var(array('header' =&gt; 'blah' ));
  }
}
?&gt;
now when you instantiate news make

Code: Select all

&lt;?
$news = new news;
$news-&gt;template = &amp;$template; //template object already instantiated
?&gt;
if you're using php5, you don't need the ampersand
Last edited by andre_c on Sat Dec 18, 2004 9:49 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Oops, yea your right I didn't mean declare :P Sorry about the confusion its been a long day. TY for words of wisdom
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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

&lt;?php
	$template = new templating();
	$db = new database();
	$news = new news();
	$news-&gt;template = &amp;$template;
?&gt;

Code: Select all

&lt;?php

		
	class news
	{
		var $template;

		function displayform($currently,$title,$body)
		{
			$template-&gt;svar(array('NEWS_TITLE' =&gt; $title,
								  'NEWS_BODY' =&gt; $body,
								  'NEWS_CURRENTLY' =&gt; $currently
			));

			$template-&gt;loadfile('newsform');
		}	
}
		
?&gt;
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

you need to do $this->template->svar ....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
Last edited by John Cartwright on Sat Dec 18, 2004 10:09 pm, edited 1 time in total.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

$this->template not $this->$template
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

someone please delete my life.... im signing off on that note

ty
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

andre_c, what's the ampersand do?

:?:
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

I'm going to have to read up on that reference thing. Time for php.net. Thanks andre_c
Post Reply