Edit object properties

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

Post Reply
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Edit object properties

Post by networkguy »

Hi, I have a question about editing object properties and subsequent database values.

If you create and save an objects properties into the database, how would you refer back to that object if in case you wanted to edit one or more of its properties. For example, lets say I instantiated a new Page() (from the Page class) object called $PageOne, set its properties of id, title and text. All I wanted to do was edit the text field, how would I go about doing that? I'm thinking about calling the setText() method and then use mysql UPDATE to update text to database.

Code: Select all

 
   1. $PageOne = &new Page();
   2. $PageOne->setText("Some text to display...");
   3. $PageOne->setTitle("My first page!");
   4. $PageOne->save(); //saves to database and returns saved id
   5.  
   6. $PageOne->setText("Some new text to display....");
   7. $PageOne>update(); //update text on page of id (using getId()) for given object
 

Should this be the right way of doing it?

Any advice would be appreciated, thanks in advance!

The other thing I did not mention for simplicity is that I'm going to want to do this from another class (lets call it Books) that will create an instance of a Page class. I want to be able to edit Page objects through the Books class, so the Books class is an interface to the Page class. I'll be using composition for this. If someone has done this type of thing before I was wondering whether you could pass the object name through the class, since the Book object would contain an array of Page objects, I'd check the name of the Page object in the Pages array of objects, if the object exists then I only want to "Update" it, else I want to instantiate a new Page object. Anyways, this part is just IF anyone has done this type of thing before. If you have any idea about this I double thank you in advance for any pointers or opinions.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Edit object properties

Post by requinix »

Eww, getters and setters.

Code: Select all

$PageOne =& new Page("My first page!"); // title is special, like a unique key
$PageOne->text = "Some text to display...";
$PageOne->save();
// ...
$PageOne->text = "Some new text to display...";
$PageOne->save();
Now, for Book:

Code: Select all

class Book {
 
    var $pages = array();
 
    function &getPage($title) {
        if (!isset($this->pages[$title])) $this->pages[$title] =& new Page($title);
        return $this->pages[$title];
    }
 
}
 
$book = ???;
 
$page =& $book->getPage("My first page!");
$page->text = "Some other text to display";
$page->save();
networkguy
Forum Newbie
Posts: 19
Joined: Tue Mar 31, 2009 2:26 am

Re: Edit object properties

Post by networkguy »

Excellent, thank you!

You confirmed a lot what I had in mind. Thank you very much for the reply again!
Post Reply