dcsimg
www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to have a PHP class in two PHP websites?

  1. #1
    Join Date
    Nov 2010
    Posts
    67

    How to have a PHP class in two PHP websites?

    I have an IIS host that has many websites (+10). I then have a global share PHP directory. Each of those 10 sites can include/require my classes/functions/etc from the global PHP shared directory.

    Is this really the right way to share my classes? It overall seems to work good. But my main issue is whenever I move code from one server to another I always manually need to go find the files from my shared function and copy/paste... Its not only more work. but can turn into a mess if I want to edit one of the copied versions and forgot to edit the other.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Not sure if I completely follow you, but I think you want to avoid changing the "common" files if at all possible. With classes, this can be rather simplified by extending one of the common classes in a local class where you need some specific change for it to work in that one application. Then that application instantiates the child class instead of the parent (common) class. (So, your local class definition would require() the common class file, then extend it and override methods as needed to handle the specific requirements of your site-specific code.)

    Or am I missing your point entirely?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by NogDog View Post
    Not sure if I completely follow you, but I think you want to avoid changing the "common" files if at all possible. With classes, this can be rather simplified by extending one of the common classes in a local class where you need some specific change for it to work in that one application. Then that application instantiates the child class instead of the parent (common) class. (So, your local class definition would require() the common class file, then extend it and override methods as needed to handle the specific requirements of your site-specific code.)

    Or am I missing your point entirely?
    My problem is I got classes that I want to use in my different websites. Lets assume I have a class called "CreateTable" in a file called "CreateTable.php". Other then adding new features to that class, or possible fixing small random bugs that would never break anything, I do not edit it. But if I do edit it, id want every single one of my projects with that class to have the latest versuin. I do not want to copy/past the "CreateTable.php" into each project, as this would turn into a mess each time I fixed a potential bug and/or added more functionality to that class.

    My solution to this has been to use include_path in PHP.INI, so every single website has access to the folder that contains "CreateTable.php". What im asking is, if that is the way to go about this? I just want to be able to 'include' classes/functions into any website without having to copy/paste (duplicate) the PHP files.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    Sounds okay to me (if they're all on the same local file system -- it gets a bit ugly if you have to access them via HTTP or such).

    The other option is to use some sort of code versioning system (e.g. Git), store all the common code in a separate repository, and then any time you update it, you can just pull the latest updates from that repository onto each site. But having all the apps on the same server simply include from the same directory tree for the common code would seem simpler and cleaner, as long as that's a viable option.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Oct 2010
    Location
    Ohio
    Posts
    233
    If you use include_path, and all your apps point to the same file, then all you have to do is update that file. No need to duplicate it. But you have to be including the same file from each app, in order for updating that file to affect each app.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles