SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    424
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    PHP Class/Object: Categories and packages

    Hi,

    I sometimes see in code commenting for PHP classes a category and package declared. What are they for? What's the difference?

    I would guess a package means all classes in it need one another to function whereas a category is just a way of breaking down large numbers of classes into groups.

    If that is correct do you normally have categories and packages in their own folder—or is there some kind of naming convention?

    Thanks.

  2. #2
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    I dont think there are any practical differences, at least not in PHP. Theoretically though, a package is a collection of classes in the same folder/directory, and a category is a collection of packages in the same parent/outer folder/directory. The concept is much more useful in Java, in which package is actually part of its language structure/syntax. PHPdoc is based on JavaDoc while the significant difference in PHP and Java's functionality makes it somewhat confusing.

    And nope, there is no need to have categories and packages in their own folder, its a matter of choice. In PHP you can use this concept called Namespace, which can organize folders much better, it also makes class autoloading much simpler.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    424
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    I dont think there are any practical differences, at least not in PHP. Theoretically though, a package is a collection of classes in the same folder/directory, and a category is a collection of packages in the same parent/outer folder/directory. The concept is much more useful in Java, in which package is actually part of its language structure/syntax. PHPdoc is based on JavaDoc while the significant difference in PHP and Java's functionality makes it somewhat confusing.

    And nope, there is no need to have categories and packages in their own folder, its a matter of choice. In PHP you can use this concept called Namespace, which can organize folders much better, it also makes class autoloading much simpler.
    Thanks so category could be /crm/

    And then inside gui the following packages:

    /crm/users/
    /crm/login/
    /crm/subscription/

    Each folder having one or more classes. Is that right?

    Finally, is there a standard way of saying that a class needs another class or function(s) to run?

    Thanks.

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Yes thats how it works in theory, but again PHPdoc's category and package only serve as class organization system, its a matter of choice whether to follow this pattern.

    It does not seem to me that PHP has this @inject annotation yet like Java does, but you can always use explicit comments to make it clear that your class needs dependency injection from another class.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    424
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'm thinking at this stage, make sure that any dependencies are all part of the same package.

    To save extra folders, do you ever do:

    PackageName.ClassName.php

    Thanks for your input.

  6. #6
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    No worries, I am glad to help. Its good to know there are this many OO programmers on this forum.

    umm in PHP I doubt thats the way it works. If you are using namespace, your class' fully qualified name will be something like Framework\Category\Package\Subpackage1\Subpackage2\...\ClassName. In this way you can easily organize your classes and create a generic autoloader that replaces the namespace operator "\\" with directory operator "/" to load your classes from the appropriate folders.

  7. #7
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    424
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    No worries, I am glad to help. Its good to know there are this many OO programmers on this forum.

    umm in PHP I doubt thats the way it works. If you are using namespace, your class' fully qualified name will be something like Framework\Category\Package\Subpackage1\Subpackage2\...\ClassName. In this way you can easily organize your classes and create a generic autoloader that replaces the namespace operator "\\" with directory operator "/" to load your classes from the appropriate folders.
    Sorry, I meant doing it that way instead of using namespaces.

    Thanks again, I'm wiser for it.

  8. #8
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    I see, yeah you definitely can do that, like I said its a matter of choice. I prefer using namespace though since it makes autoloading much easier and faster,


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
  •