PHP and object!

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
giov85
Forum Newbie
Posts: 3
Joined: Tue Dec 14, 2010 10:56 am

PHP and object!

Post by giov85 »

Hi to everybody!!! It's the first time i'm here, so sorry for my bad english ;)
I have a little (for you) question, that make me crazy!

I would begin to make class in php. I know the base of php5 class, but i never use it on a project. I think that the big problem in my mind is how I have to implement the method.

Let's see an example. Now, if I want to make a form to insert i new, I write something like it

Code: Select all

<form name='newsForm' id='newsForm' method='post' action='page.php'>
<input type='hidden' name='newsId' id='newsId' value='<?php echo $newsId; ?>' />
Title: <input type='text' name='newsTitle' id='newsTitle' value='<?php echo $newsTitle; ?>' />
...
<input type='submit' name='newsSave' id='newsSave' value='Save it!' />
</form>
with newsTitle, newsId and the other (lot's of) field read from a db.
Then i write a function like it

Code: Select all

if(isset($_POST['newsSave'])){
$newsId = $_POST['newsId'];
$newsTitle = $_POST['newsTitle'];
...
$sqlUpdate = "UPDATE news SET title = '$newsTitle', ... WHERE idNews = '$newsId'";
}
(I sanitize the input to prevent MySQL Injection in this phase).

But how should I write this little thing in object? I really don't understand it! I have to write a method like printForm() where i write the code to print the form that i wrote before?
And then in page.php create a new object and call the method?
And to save? I have to do the same! And so, where is the power of the object? In this way is like lot of include!

I'm sure that there is somethink that i can't understand... Can anybody help me?
Thanks!!!!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP and object!

Post by Jonah Bron »

The concept behind OOP is not to wrap procedural code, but to represent objects, their data, and their behavior. At this point, the number of objects that come up in an application like this is so small, you don't need it. But if you start working on a larger project (or make this one larger), I strongly recommend that you learn how OOP works.
giov85
Forum Newbie
Posts: 3
Joined: Tue Dec 14, 2010 10:56 am

Re: PHP and object!

Post by giov85 »

Thanks Jonah!
other question: do you know where i can find a good tutorial where i can understand how to use it?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP and object!

Post by Jonah Bron »

You can start with the Wikipedia page:

http://en.wikipedia.org/wiki/Object-ori ... rogramming

Other than that, I don't know of any good articles on it... some google-fu would be in order.
Post Reply