Front-end and back-end shared or separate libraries?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
letseatfood
Forum Newbie
Posts: 9
Joined: Mon Aug 02, 2010 3:50 pm

Front-end and back-end shared or separate libraries?

Post by letseatfood »

Is it good practice to separate the front-end PHP library from the back-end?

It seems, at the moment, better to share a library between the front-end and back-end.

I am curious to hear opinions about this.

Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Front-end and back-end shared or separate libraries?

Post by John Cartwright »

All my applications on a given server share the same library. However, in the context of one application, yes it is a good idea to share your library between different "modules". It will certainly make things a lot easier when upgrading given libraries, but it does come at the cost of potentially breaking functionality elsewhere (which is why it is important to unit test!).
letseatfood
Forum Newbie
Posts: 9
Joined: Mon Aug 02, 2010 3:50 pm

Re: Front-end and back-end shared or separate libraries?

Post by letseatfood »

@John - Thanks for your response. I can see how "potentially breaking functionality" could be an issue, but with proper testing, prevented.

Two related questions I have about sharing my current library with two or more "modules" are:
  1. Where to place the library directory within the server?
  2. How to refer to the files within the library? For example, absolute or relative URLs?
For example, I currently have the following structure:

Code: Select all


[root]
  |
  |- [admin]
  |    |
  |    |- [lib]    <−− Library I would like to share
  |    |- index.php
  |
  |- index.php

Do you think I should move the [lib] directory, which I would like to share with other modules within the [root] directory into the [root]?

I appreciate any advice you might give.

Thanks!
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Front-end and back-end shared or separate libraries?

Post by PHPHorizons »

Hello letseatfood,
Where to place the library directory within the server?
There are really two main places for the library to go. One: above the web root folder. Two: inside the web root folder (or one of it's subfolders). If you can put the library above the publicly accessible web folders, that is best. Otherwise, it doesn't matter too much where the library is.
How to refer to the files within the library? For example, absolute or relative URLs?
I prefer using absolute *paths*. I do not use URLs at all for including files. URLs can be used, but if the files are on the same server, use a file path instead. It's far more secure and efficient. Either way, use an absolute path/url. I define a constant that points to my library folder. Then, anytime I need a library, it goes like this:

Code: Select all

PATH_LIBRARY . 'some_particular_library' . DIRECTORY_SEPARATOR . 'some_file.php'
Cheers
letseatfood
Forum Newbie
Posts: 9
Joined: Mon Aug 02, 2010 3:50 pm

Re: Front-end and back-end shared or separate libraries?

Post by letseatfood »

@PHPHorizons - Thanks a lot! Is `DIRECTORY_SEPARATOR` a constant that holds a forward or backward slash?
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: Front-end and back-end shared or separate libraries?

Post by PHPHorizons »

You're welcome letseatfood,
letseatfood wrote:Is `DIRECTORY_SEPARATOR` a constant that holds a forward or backward slash?
Absolutely. It's handy when a script might be used on Windows or Linux, since this value is tied to the operating systems own directory separator.

Cheers
letseatfood
Forum Newbie
Posts: 9
Joined: Mon Aug 02, 2010 3:50 pm

Re: Front-end and back-end shared or separate libraries?

Post by letseatfood »

@PHPHorizons - Thanks! I had never thought about that, what a useful tip.
Post Reply