Recursion(?) in My Class

This is a discussion on Recursion(?) in My Class within the PHP Language forums, part of the PHP Programming Forums category; I'm about to tread on some very thin OOP ice, and I could use a little In my application, ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-18-2007
Sanders Kaufman
 
Posts: n/a
Default Recursion(?) in My Class

I'm about to tread on some very thin OOP ice, and I could use a little

In my application, I have a publications class that has both a
"PublicationEditor" and a "PublicationViewer" function.

Since I don't want to confuse the publication I'm viewing, with the
publication that I'm editing, I want to create a new instance of my
Publication Class in the PublicationEditor.

Code:
class Publication() {
var $Title;
var $Content;
funciton Publication() {
$this->Title = "Hello World";
$this->Content= "Goodbye, world!";
}
function GetPublication($iID){
//code to get publication title, content
return true;
}
function SavePublication($iID, $sTitle, $sContent){
//code to get publication title, content
return true;
}
function ViewPublication($iID) {
echo $this->Title;
echo $this->Content;
}
function EditPublication($iID, $sTitle, $sContent) {
$oPub = new Publication();
return $oPub->SavePublication($iID, $sTitle, $sContent);
}
}
(The code is seriously dumbed down to represent my question :) )
My question is basically this:
Is there any problem with the way I'm creating a new publication ($oPub)
from within EditPublication, bearing in mind that I know not to call
$oPub->EditPublication?


Reply With Quote
  #2 (permalink)  
Old 09-18-2007
NoDude
 
Posts: n/a
Default Re: Recursion(?) in My Class

Why would you create a new publication in the edit method? Can't you
have a separate method that's called addPublication? You may know not
to call EditPublication (at least untill some time hass passed and you
forget), but imagine my (or any person's) surprise when I one day take
over this application, use this class and edit is actually used for
create.

Reply With Quote
  #3 (permalink)  
Old 09-18-2007
Sanders Kaufman
 
Posts: n/a
Default Re: Recursion(?) in My Class

NoDude wrote:
> Why would you create a new publication in the edit method? Can't you
> have a separate method that's called addPublication? You may know not
> to call EditPublication (at least untill some time hass passed and you
> forget), but imagine my (or any person's) surprise when I one day take
> over this application, use this class and edit is actually used for
> create.


That wasn't real, live code.
It was just a representation of a concept.
The actual code file is a couple of hundred lines long.

But the reason why I think I need a new Publication object is because
it's a web page that lets you edit other web pages. Thus the current
object isn't the one you're editing, but rather the one you're viewing.

I (think I) want to create a new instance of the publication object,
because I'm editing a whole different publication.

I think this is called MVC - Model View Controller, but I don't know why.
Reply With Quote
  #4 (permalink)  
Old 09-20-2007
burgermeister01@gmail.com
 
Posts: n/a
Default Re: Recursion(?) in My Class

On Sep 18, 8:06 am, Sanders Kaufman <bu...@kaufman.net> wrote:
> NoDude wrote:
> > Why would you create a new publication in the edit method? Can't you
> > have a separate method that's called addPublication? You may know not
> > to call EditPublication (at least untill some time hass passed and you
> > forget), but imagine my (or any person's) surprise when I one day take
> > over this application, use this class and edit is actually used for
> > create.

>
> That wasn't real, live code.
> It was just a representation of a concept.
> The actual code file is a couple of hundred lines long.
>
> But the reason why I think I need a new Publication object is because
> it's a web page that lets you edit other web pages. Thus the current
> object isn't the one you're editing, but rather the one you're viewing.
>
> I (think I) want to create a new instance of the publication object,
> because I'm editing a whole different publication.
>
> I think this is called MVC - Model View Controller, but I don't know why.


Sanders,

I'm admittedly a bit confused by your question, so I'm not sure if
what I'm about to say will help you at all, but here goes.

Firstly, I concur with NoDude that it is a bit misleading to have an
instantiation of a class in an edit method. I'm assuming these
publications that you speak of are being coordinated with a database,
and therefore I think good solution to your problem may be a design
pattern known as Active records. Check it out: http://en.wikipedia.org/wiki/Active_record
A way that I implement this concept in PHP all the time is like this:
put a parameter in your class constructor called $id and have it
default to NULL like so

funciton Publication($id = NULL)

You will then want to pull a record from the DB or instantiate a new
class based on the value of $id. So when you go to your edit page,
just pass in the appropriate parameter to the constructor, make any
edits that need to happen to class members ($title, $article) and when
you're done, call the SavePublication() method. Of course you can now
remove the $id parameter from your SavePublication() signature because
that value has already been stored in the class.

Anyways, I hope I'm not rambling on about some concept you already
know, and furthermore I hope this concept can help you with your
problem.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:33 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0