Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts

    How to use classes

    I've had some experience programing with php and have been working with some open source applications. I have noticed that all of these classes use classes. I have heard of classes before and know that they are used in OOP, but what is the advantage and how would I use a class once I had it built?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Purpose of a class can be summed up in one word: encapsulation. This means any data relevant to the actual object in question is all stored within that object. I'll use the classic user as an example. A user is an object with two attributes, a userid and a username. Any user stored in the system is a type of user object with the corresponding userid and username, and these attributes can be accessed within this object:
    PHP Code:
    class PHP5_User
    {
        private 
    $userID;
        private 
    $username;
        public function 
    __construct($userID$username)
        {
            
    $this->userID $userID;
            
    $this->username $username;
        }
        public function 
    getUserID()
        {
            return 
    $this->userID;
        }
        public function 
    getUsername()
        {
            return 
    $this->username;
        }
    }

    class 
    PHP4_User
    {
        var 
    $userID;
        var 
    $username;
        function 
    PHP4_User($userID$username)
        {
            
    $this->userID $userID;
            
    $this->username $username;
        }
        function 
    getUserID()
        {
            return 
    $this->userID;
        }
        function 
    getUsername()
        {
            return 
    $this->username;
        }
    }

    $user = new User(1'Fou-Lu'); // Choose either example for the 'User' class, 'PHP4_User' or 'PHP5_User';
    printf("Hello %s!<br />\n"$user->getUsername()); 
    Pros:
    - Easier to write and debug (big problems broken into smaller parts)
    - Portable and reusable
    - Specialized for logical data encapsulation
    - Extensions, Abstracts and scope control in PHP5

    Cons:
    - (A lot) More memory usage
    - Often more code than procedural to perform the same task

    Objects are great for reusability. If objects are correctly built, you can reuse you're code over and over again to easily create new sites with little or no modification on the logic. Generally I do not use classes and objects for any type of output unless its a template handler. Sometime in the past I shifted to an n-tier style logic which has flowed over into my PHP. This logic fits great with databases - one table generally corresponds to one object.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    ninnypants (08-06-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •