PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Recursion(?) in My Class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Recursion(?) in My Class

Réponse
 
LinkBack Outils de la discussion
Vieux 18/09/2007, 05h58   #1
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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?


  Réponse avec citation
Vieux 18/09/2007, 10h17   #2
NoDude
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Vieux 18/09/2007, 14h06   #3
Sanders Kaufman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 20/09/2007, 15h15   #4
burgermeister01@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 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 you with your
problem.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h43.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12652 seconds with 12 queries