Recursion(?) in My Class
am 18.09.2007 06:58:08 von Bucky Kaufman
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);
}
}
[/code]
(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?
Re: Recursion(?) in My Class
am 18.09.2007 11:17:06 von NoDude
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.
Re: Recursion(?) in My Class
am 18.09.2007 15:06:11 von Bucky Kaufman
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.
Re: Recursion(?) in My Class
am 20.09.2007 16:15:11 von burgermeister01
On Sep 18, 8:06 am, Sanders Kaufman 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.