How do I initiate a class?

Discussion in 'PHP' started by wwwbryan, Jun 13, 2009.

  1. #1
    I am trying to follow instructions and I need to initiate a class, I have no idea what that means.

    I am guessing it refers to this class?

    class ipb_login
    {
     
    wwwbryan, Jun 13, 2009 IP
  2. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #2
    The class is the "definition", which defines the properties, methods, etc. What you meant to write is "instantiate", which means to create an instance of the class.

    Use the new keyword to create the instance as follows:

    <?php
    $instance = new ipb_login(); //This will create an instance of the ipb_login class.
    //Now you can set/get properties and invoke methods as needed.
    ?>

    I hope this makes sense, else take a look at the basics @ http://us3.php.net/manual/en/language.oop5.basic.php
     
    Social.Network, Jun 13, 2009 IP
  3. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Thanks, I did this:

    	$instance = new ipb_login();
    	$instance = ipb_login::is_logged_in();
    Code (markup):
    And it gave me this:

    Fatal error: Using $this when not in object context in C:\wamp\www\login_api.php on line 364
     
    wwwbryan, Jun 13, 2009 IP
  4. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #4
    $instance is an instance of the ipb_login class. Set/Get properties and invoke methods against the $instance object.
     
    Social.Network, Jun 13, 2009 IP
  5. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    I'm sorry what?
     
    wwwbryan, Jun 13, 2009 IP
  6. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    $instance = new ipb_login();
    $instance->is_logged_in();
     
    stOK, Jun 14, 2009 IP
  7. wwwbryan

    wwwbryan Well-Known Member

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Yeah...
    But that doesn't let you use that code in functions.

    And you can't repeat the first line more than once.
     
    wwwbryan, Jun 15, 2009 IP