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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Understanding Classes

    Can someone possibly explain how php classes work? I'm looking to add a function to an existing php class. Should the function be ran automatically when the class file is called?

    Or do you need to include the class file, and then run the function separately?

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Functions that are run when the class is created are called contructors. These can accept parameters by adding parentheses after the class name. If you just want to add functions without them having run automatically, it's pretty much the same as normal but within the class.

    PHP 4
    PHP Code:
    // Constructor
    class constructClass 
    {
      var 
    $variable;

      
    // The constructor is the function with the same name as the class
      
    function constructClass $parameter 
      {
        
    $this -> variable  =  $parameter;
      }
    }

    $class = new constructClass'hi' );

    echo 
    $class -> variable;
    /* The -> allows you to access variables and functions inside a class
    This example we are accessing the variable $variable inside the $class instance of the class */ 
    PHP 5 is slightly different

    PHP 5
    PHP Code:
    class constructClass 
    {
      public 
    $variable// Here the 'public' keyword is used as var is decrepit, 
      // public means you can access the information from outside the class

      // The constructor is the function with the name __construct
      
    public function __construct $parameter 
      {
        
    $this -> variable  =  $parameter;
      }
    }

    echo 
    $class -> variable

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    That confused me a little bit. But I'm actually working php 5, and have a class that includes function __construct() at the beginning of the class.

    Do I understand correctly that if I want to have the function ran automatically that I need to add a constructor within the function __construct() ?

  • #4
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Anything within the __construct function will be run automatically when you create a new instance of the class.

  • #5
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    I just started learning OOP (like 3hrs ago lol) so correct me if I am wrong, But I am pretty sure you are only allowed ONE construct function per class in php OOP.

    also the construct class could be either : __contstuct(), Or it could share the same name as the class, ex my class name = test, so constructor function is test(). Using __constuct is new to PHP 5 and is the more favorable way of doing things.
    Last edited by srule_; 12-24-2007 at 09:21 PM.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ptmuldoon View Post
    Can someone possibly explain how php classes work? I'm looking to add a function to an existing php class. Should the function be ran automatically when the class file is called?

    Or do you need to include the class file, and then run the function separately?
    There are two kind of function:
    - function what use objects and don't belong to them
    - a function what belong to a class, called method
    both could be called inside constructor.

    You can modify the source of the class to do what you want, but this is
    bad practice, usualy you must extend the class to fit your need.

    I think that could be more easy to help if you give an example about what you want to do.

    best regards and happy holidays.


  •  

    Posting Permissions

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