i really can't understand what does the class significance. Some programmers using class to create object. But we can create objects just using function. Is class really that significant in php programming?
Creating your own classes (and thus being able to create objects from these classes) is the basis of Object Oriented programming.
It is an extremely powerful part of the Object Oriented programmming which allows you to re-use and incapsulate functionality so that you have something that represents whatever it is you are doing.
(Sorry, it takes a bit of study to really "get it". Just learning the basic syntax does not teach you how to use object-oriented design and programming.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
zimonyi did an excellent job explaining the high level "why" OOP is considered by developers vs. other means. Compliments to NogDog for posting outstanding links to help novices learn about the conceptual aspects of it - this is the kind of thing where RTFM really matters!
Jim, Sr. Web Developer You know who you real friends are when you ask them to move your furniture or paint.
I will also give a fairly good example of when Object Oriented programming is in its essence.
I have a site created in PHP. Like most server-side programming sites, which can use any server-side language, they have a database which contains a bunch of data.
Long ago I worked with ASP, and there was three objects there that was very easy to use when working with databases. They were called Connection, Command, ResultSet.
The Connection does what you think it does, it contained the database connection.
The Command object contained the SQL statement and you could set the statement and then simply run execute() on that statement.
The ResultSet contained the result of your query.
The above is simple and elegant.
When I started with PHP (around the same time) they only had the mysql_connect(), mysql_query() and mysql_fetch_array() as specific methods for working with the MySQL database or for say the postgres database they had pg_connect(), pg_query() and so on.
This means that if you have a lot of PHP pages you need to implement database specific functions in all your pages. If for some reason you need to change the database you need to do that for all your pages on your site.
What I did was to create the above mentioned classes for my site in PHP. I had a single method connect() which used mysql_connect() when I (in the beginning) used the MySQL database. When I later switched I only had to make my change in one place, namely in my Connection class, and I changed it to pg_connect().
Same with the Command class. It had a setStatement() function and then an execute() function. When I changed my database I only needed to change the PHP function names in my execute() function for it to work with the new database.
The code for the actual pages on my site still looked the same, no changes at all.
Bookmarks