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.
Or are you asking about something else?
If you are new to the world of object-oriented programming (OOP) and OOP PHP, start here for the basic concepts: http://java.sun.com/docs/books/tutorial/java/concepts/. Then check out the PHP specifics at http://www.ibm.com/developerworks/op...w01PHP5objects and http://www.ibm.com/developerworks/op...y/os-advphpobj. Then get hold of Matt Zandstra's book PHP 5 Objects, Patterns, and Practice to get it all tied together and start thinking in OOP terms.
(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.)
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.
ill read those links then. thank you for advices guys... =)
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.
If done right, it could indeed be very powerful.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)