Page 1 of 1

PHP and object!

Posted: Tue Dec 14, 2010 10:58 am
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!!!!

Re: PHP and object!

Posted: Tue Dec 14, 2010 11:01 am
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.

Re: PHP and object!

Posted: Thu Dec 16, 2010 4:25 am
by giov85
Thanks Jonah!
other question: do you know where i can find a good tutorial where i can understand how to use it?

Re: PHP and object!

Posted: Thu Dec 16, 2010 11:10 am
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.