SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot bopjo1's Avatar
    Join Date
    Jun 2007
    Location
    Tampa, FL
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie question about static methods

    My goal this year is to try and wrap my head around PHP OOP. One thing I've been noticing is that you can apparently access public static methods from another Class from within a different Class without including the first Class file. Is this correct? I'm so used to including a file first before you can access any functions or variables from it, but with Classes, one can apparently use static methods without including them first. Do the Class files need to be in the same directory for this to work?

    So many questions....such limited brain capacity...

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, this is not exactly correct.
    Yes, you can access public static method of one class from another class but you still need to include that class using any of the include techniques: require or include or rely of autoloader function to find and include that class for you.

    File containing that class has to be included one way or the other.
    Remember in php you can have multiple classes per file, it's up to you. Usually it's a good design practice to write one class per file, but this is not Java, and you can even write all your project's classes in one very big file.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Zealot bopjo1's Avatar
    Join Date
    Jun 2007
    Location
    Tampa, FL
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that clear explanation. The book I'm reading creates several files that use static methods from other Classes, but I looked ahead a few pages and saw that they are all included in an index.php file using the MVC pattern.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to instantiate an object, but as mentioned the file must be included.

    Does this book include ALL the classes used in the app in index.php automatically?

  5. #5
    SitePoint Zealot bopjo1's Avatar
    Join Date
    Jun 2007
    Location
    Tampa, FL
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It includes them using "require", but does not auto-load them, if that's what you mean.


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
  •