PHP classes/functions

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I have been looking at the use of PHP classes/OOP.
From the tutorials I have read so far, and from some of the more simple ones, I see no real difference between declaring a class and using a user defined function.

Both seem to serve the same purpose, a block of code that can be called on multiple times to avoid large amounts of repeat code.
I guess classes will have differences to functions, certain usages, (global) variable handling perhaps etc, but what is the difference in simple usage?
For example

<?php
   
class dummy {
        var
$variable;
        var
$variable2 = 1;
        var
$variable3 = 2;
        function
sum($one, $two) {
           
$val = $one + $two;
            return
$val;
        }
?>

A function would do the same thing, so which would be better?

Also, something else I have wondered..
Functions are usually in a separate file, for example a database connection function would be used in many pages, and so would be in an include file.
So say you include that db connect file in a login.php file. PHP has to access login.php, read the include() and then access the hard drive to access another file.
Isn't this second accessing of the hard drive for the include file going to be slower than simply having the database connection code in the login.php file?

I know we could be talking of 1000ths of a second, but as large sites use this, with tens of thousands of members accessing files included on all pages, member permission checks for example, surely then it would have a notible impact having say 5 thousand people accessing two hard drive files rather than one?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

As I'm the type who learns off of actual examples and using them, classes were hard for me to get as well. All the "simple" examples just seemed like overkill for things like websites. As you said, an included function file would work just as good.

It wasn't until I started reading about the differences between PHP 4 and 5's classes that I found a use for them. In 5, a classes information is actually protected, where in 4 it wasn't (ie, in your code, in 4, you can directly access the values of $variable, $variable1, $variable3, where in 5, if the class is defined right, your code that uses the class cannot).

As for the including of files, for this you have to understand how a server handles calls to the file system and caches that information. If you have a major site that is calling a database connection every single page, the system will most likely cache that included file in memory and not actually read the drive.

Also, it's been a while since I looked into it, so don't remember if it is a setting in PHP or one of the zend products, but there is a way where not only will the code be cached, but you can have it cache the "compiled" code for faster processing (PHP compiles line by line as it is used, and it will save chucks of the final machine code to use over instead of recompiling the lines.

It amazes me when I stop to think at what all happens behind the scenes when you call a website anymore. Heck. I remember back 14 years ago, we thought it was soooo amazing to have a BBS system that could handle 64 different people dialed into the one computer and how well it handled all 64 accessing the same database at the same time (see http://tbbs.org/TBBS.html). There is a tremendous amount of file control and processing that goes on on computers. [/oldfartmode] lol

-The Other Greg

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Greg K wrote:
If you have a major site that is calling a database connection every single page, the system will most likely cache that included file in memory and not actually read the drive.
....but you can have it cache the "compiled" code for faster processing.....

Ahh, I will look into that. I suppose, as you said, it's behind the scenes I need to look at. When php will determine it's worth caching something and how it does it. How many accesses in a given time or whatever.
Sounds like something that could be very worth while that in certain sites and circumstances could return noticable performance improvements.

Greg K wrote:
It amazes me when I stop to think at what all happens behind the scenes when you call a website anymore.

Yeah.
Having compiled my own kernal in linux, played with apache/php/mysql installs myself, code php/css etc and have a bit of understanding of the way servers work, DNS registrars, nameservers, ISP IP lease and DHCP etc, it is mind boggling that the internet actually works Laugh

Most simple company networks with a few hundred PC's and a mainframe have major faults and issues, but a huge public one with many hands in the pie and outside influences on anything we do...it is amazing really

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

A function would do the same thing, so which would be better?

Neither. I don't think there's any need to abstract addition Smiling.

I used to be a function writer guy too, but once I got acquainted with OOP, I just can't go back. Having a class that abstracts an entity is really nice. You have just one little object variable that holds all the data about an entity, and holds all the methods that can manipulate that entity. All in one neat little package. You just can't beat that.

Say you're running a dog website. Wouldn't it make sense to create a class that holds all the dog's information, and a set of methods that do something to the dog?

<?php
class Dog
{
      private
ID;
     private
name;
     private
description;
     private
breed;
     private
color;

     function
__construct($ID)
     {
         
// get dog row from database
          // assign that tuples to class variables
    
}

     function
getName()
     {
          return
$this->name;
     }

     function
setName($name)
     {
         
// change name in database
          // change name in class variable
    
}    

}
?>

Even better yet, you could create a general animal class and have each Dog, Cat, Bird class inherit from the animal class. That would cut down on your code duplication quite a bit. Your app would be a lot easier to maintain.

Isn't this second accessing of the hard drive for the include file going to be slower than simply having the database connection code in the login.php file?

Yup. But say you change your database from MySQL to Postgre (is that how it's spelled?). Now you have to change every file in the site that accesses the database because you didn't abstract the database accessors.

Maintainability trumps speed in most cases. First make your code readable, maintainable, and reusable, and then refactor it for speed (when it's needed). I think most programmers would agree. That's a common theme in a lot of the programming books I've read.

greg's picture

He has: 1,581 posts

Joined: Nov 2005

Hmm, it looks like I really should investigate classes a bit more.
Although, I am a believer in only using things if necessary, rather than it's there so use it. So as with functions, I will only use classes when it will be worthwhile.

People use functions too much imo, for the sake of a few lines of code used 2-3 times it's not always worth it.

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

A correction of the first post code (missing bracket):

<?php
   
class dummy {
        var
$variable;
        var
$variable2 = 1;
        var
$variable3 = 2;
        function
sum($one, $two) {
           
$val = $one + $two;
            return
$val;
        }
    }
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.