1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using Classes in PHP

Discussion in 'PHP' started by vijaykoul, Feb 13, 2005.

  1. #1
    Hi everybody,

    I was trying to work with classes in PHP
    Although they are similar to in making to classes in other languages, but I faced one problem.

    The code for the class is as follows:

    <?php
    class Page {
    var $Title;
    var $Keywords;
    var $Content;

    function disp( )
    {
    echo "<HTML>\n<HEAD>\n";
    echo "\n</HEAD>\n<BODY>\n";
    $this->Title="My First Page";
    echo $this->Title;
    echo "\n</BODY>\n</HTML>\n";
    }
    function main()
    {
    $objP= new Page;
    $objP->disp( );

    }
    }
    ?>

    I saved thisfile as classes.php and tried to run it, however I got no output.
    Then what I did was that I saved this file as '.class' file and created another file named P1. php.
    Code of that file is as follows:
    <?php
    include "classes.class";

    $objP= new Page;
    $objP->disp( );
    ?>

    this time it worked fine and i also got the output right.
    I have a few questions in mind.
    1. Is it necessary to create '.class ' file and then call it in other file to execute classes in PHP
    2. Is there anything wrong with my coding in classes.php file that maked it not work exactly I wished to.

    or is there any other problem.

    I'll be highly thankful to anybody whos provides me its solution

    vijay
     
    vijaykoul, Feb 13, 2005 IP
  2. Ozz

    Ozz Peon

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi vijaykoul. What I can see is that you are defining the class inside the class. Remove function main() from inside the class. The extension should be .php for the class to be used as an include.
     
    Ozz, Feb 14, 2005 IP
  3. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    PHP is not like Java in that it doesn't automatically execute the main() method. You can use a constructor method with the same name as the class. And the extension shouldn't matter, it can be class or php but using the php as the extension is a good security measure.

    Also, my PHP class knowledge is only of PHP 4.x. PHP really overhauled classes and OOP in PHP 5 to make it compatible with OOP standards. PHP 4.x OOP is just a little hack they threw in at the last moment and has very limited OOP capabilities.
     
    rvarcher, Feb 14, 2005 IP
  4. vijaykoul

    vijaykoul Guest

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thankx for your replies. i needed the answer very badly as i want to do some serious OOP based programming using PHP. So the best way is to start from classes
     
    vijaykoul, Feb 14, 2005 IP
  5. Juls

    Juls Well-Known Member

    Messages:
    1,867
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    170
    #5
    like rvarcher said you can place your classes in php files for security reason as no one will be able to read them versus having them a .class files. but the benefit of having external class files versus including them with your normal php code is so that it is makes them versatile in the sense that you can easily use them with other scripts or application that you make in the future.

    and you always will have to do it like this:

    
    <?php
    include "classes.class";
    
    $objP= new Page;
    $objP->disp( );
    ?>
    
    ;-)
    
    Code (markup):
     
    Juls, Feb 16, 2005 IP
  6. frud0

    frud0 Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yup, PHP, in a nutshell, would try to compile everything thats marked as PHP code. Wont specifically run a function if its titled/named "main" first... infact it wont run it at all if "main" aint "called", somewhere in the PHP code. Also its best to use includes TO KEEP THE CODE NEAT, trust me it matters a lot when you`re coding LARGE scripts.
     
    frud0, Feb 20, 2005 IP
  7. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #7
    PHP5 supports a new language construct to define an object's constructor. Also, supposedly, the new object system takes a lot from Java, so anyone used to that should pick it up quickly.

    Generally, it's good practise to give each class its own file, and the filename should contain the name of the class.
     
    nullbit, Feb 21, 2005 IP
  8. Juls

    Juls Well-Known Member

    Messages:
    1,867
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    170
    #8
    nullbit in php5 you dont define the object's construct but you call the method "__construct"... however i am not sure if using the same name as the class will still work or not.
     
    Juls, Feb 22, 2005 IP
  9. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah. I guess they changed it before the first final release. I'm positive it *did* exist. I still use PHP4.
     
    nullbit, Feb 23, 2005 IP
  10. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #10
    In php5 the __construct and a programmer defined constructor will work, they left the php4 version in for backward compatibility.
     
    palespyder, Mar 4, 2005 IP
  11. dtan

    dtan Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    the biggest thing to keep in mind (especially if coming from a java background), is that in php4 objects are not referenced. They are always passed by value as a copy unless explicitly used with '&'. In php5, the object model changed significantly and all objects are references. . .like java.
     
    dtan, Mar 11, 2005 IP