SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Saving Classes

  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Saving Classes

    I have a class Class1 saved in Class1.php in the /classes directory.

    If I create Class2 extends Class1, should I create Class2 in the Class1.php file or create it in Class2.php?

    If I create it in Class2.php, how do I reference Class1 when I instantiate a new object of Class2?

  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either way works, but 99% of the time I prefer to put each class in it's own file. The only time I break that rule is when I have a second class that is needed by (and only by) the first class.

    As for calling it, you just need to include both Class1.php and Class2.php, then initiate the object, i.e.
    $class2 = new Class2;

    It might be a good time to get autoload going (if you're not already using it) as you'll find the more classes you write the including will grow to the point of ridiculousness.
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again. Makes sense and yes I agree on autoload. I have an Advanced PHP5 book that goes into that so off I go.

  4. #4
    SitePoint Evangelist
    Join Date
    Nov 2009
    Posts
    466
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's much easier. Works great. Thanks again.

Bookmarks

Posting Permissions

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