Local include path alias - php

This might be a silly question but it really annoys me. I began to program in Sublime Text 2 a short while ago, and really love it. One thing I just don't know how to set up is a way to include scripts that has to be included differently on the server.
On the server I have an include library, where I save all the "secure" files. This directory is placed outside of root, but using Apache, the PHP script can access the scripts simply by writing include "filename.php";. I really love this feature but it prevents me from receiving documentation and list over functions and variables. If I want to receive these informations, I have to include my classes/files like this: include "../path/filename.php". I could do this, but then it won't work on the server.
I know this is silly, but I really think it is to much work to comment out a path variable every time I have to upload, for test and then uncomment it again when writing code. I hope you understand what I am trying to achieve, and please tell me if there is an easy solution. I mentioned Sublime Text 2 because I know there is a lot og packages (plugins) and perhaps somebody have thought about a plugin that automatically checks every include/require command and if no path is defined, it checks whether or not it is in the local folder and again if not, it checks a ".settings" file for a custom path I manually have defined. That would be nice :D

http://php.net/manual/en/function.set-include-path.php set_include_path is what you need. YOu can set it either in php.ini or in php

Related

List all loaded files in PHP

There is no way on denying that PHP is based on includes. Even PSR4 is responsible for including files from folders so, so far away.
Sometimes it become very difficult to debug code, mainly on third party code, don't matter how good it was written.
With PHP being so dynamic in function calling it is very easy to get lost. And the URL don't help much in software like MOODLE.
Is there a way to tell PHP to error_log every file that was loaded in a request? So I can know the files i need to debug.
I don't think using the PHP magic constants will help much.
get_included_files()
This function returns all required and included file names as array.

Open source software - How to setup files

I'm in the middle of making my own custom forum system software. Much like phpbb, mybb, vbulletin, etc. except it's obviously quite less advanced. It's just a personal project for myself and I've run into some problems since I've never had to develop something that can be repackaged for others.
The file structure is as follows:
So, config.php is the end all be all of including files. It has the database connection information, it instantiates my database class as well, and none of the function files require/include any files since they'll always be accessed where config.php is required.
HERE'S THe QUESTION!
However I'm running into simple but very annoying problems, for example I call a function in config.php towards the top that checks the users cookies values and makes sure they all belong to the same user, and if not it deletes the cookies. However, it has to be after the database files require. And things like, a variable declared in config.php isn't always accessible, so sometimes I have to declare it in the header files.
Seems like it's not much of a question, but I guess it's just asking for how I can include/require in general without running into issues.
As a general note, most people don't mix config variables and code in one file. If you look at popular open source packages like Wordpress, Config.php just has config variables set. No code.
If you're using certain functions in anything more than a "one off" situation, you may want to consider putting them into your main class - that way they're available as needed.
#James is right, separate your config file. You can include it inside an "application.php" required file (so it's available globally).
I have run into a situation where I absolutely needed HTTP Header information prior to page build. Though it seemed a little backward, the solution was to call that file first, then include the application.php file. Checking for a cookie should be fine.
In another situation, #include('myStubbonPricing.php') was the answer. I'm not an advocate of error suppression, but in my case it only outputted a shipping rate (if the zip code was entered). To my defense !isset and the like would not fix the problem due to an XML request/response scenario.

Deleting large chunks of PHP effectively

I've just inherited a project, and been told that an entire folder, "includes/" needs to be removed due to licensing issues -- We don't have the right to redistribute the files in that folder, so we need to cut our dependencies on them, and fix whatever breaks. I've been told "Less than 5% of the lines in that folder are ever even called by our program", but I have no way of verifying this.
There are about 50 files in the folder, each with a couple hundred lines of code. There is no unit testing currently in place. There's one master file, include.php, that require()s all 49 other files, so I can't just grep for any file doing import() on includes/.*.
This is about as much detail as I've really figured out at this point. I spent all last week reading through the files in the includes/ folder, and it won't be hard to rewrite any of this, but I'm having trouble deciding where to start. I tried deleting the folder and slowly fixing things that break, but I'm afraid that this route will cause me to miss some crucial functions in my rewrite.
Can anyone point me in a direction to get started? Are there tools that will simplify this process? I'm looking at xdebug right now, but I'm not sure exactly how I'd use it for this.
You may want to search for "php code coverage." That should help you figure out what code is used. For instance, this appears like it might help:
http://www.xdebug.org/docs/code_coverage
Your initial approach isn't bad at all. It's certainly a reasonable place to start:
delete that code that isn't allowed.
try to run what's left.
if things break: create a stub for a method that is now missing, and set it to return some sensible "default" value for now.
goto 2.
Then, itemize all the things that were missing, and make a sensible schedule to re-implement each thing.
I would start by grepping for files that reference include.php. Check through them if they're manageable, one by one. Then I'd grep for each of the functions in the /include/*php files. See if they're called anywhere, find 'em, replace 'em.
Because PHP is so dynamically typed, I don't think there's going to be a tool for this.
(Eagerly awaiting someone to prove me wrong because I have similar tasks all the time... )
See SD PHP Test Coverage Tool. It will provide a visual view of what code actually executes, as well as a report on what parts of files are used (including "no parts", which is your cue that
the code is a likely candidate to delete).
It doesn't require any hand-modifications of your code, or any unit tests to run it.
To answer my own question, I wound up using xdebug profiler to do the job, as I was initially investigating (after a friend's suggestion prompted me to take a second look).
In my /etc/php5/apache2/conf.d/xdebug.ini (on ubuntu 9.10), I set xdebug.profiler_enable=1 and xdebug.profiler_output_dir=/var/log/xdebug/, then loaded up the resulting cachegrind files with KCacheGrind and just ran a search on filenames for "includes/".
Now I have a mountain of work ahead of me to remove all this, but at least I've got a good overview of what I'll be modifying!

can I use .ini for configuration file in PHP?

My friend asked me to update a PHP application that his company uses. I found out that the application uses .ini extension for DB configuration file. The file contains DB host address, username, and password!!. The problem is that I can access the file on web-browsers.
I am trying to understand why. Is there any particular reasons to use a regular php file with .ini extension??? I just don't get it.
Readability is one of the main reasons I've used ini filies for php script configs in the past. People who are not coders have run into an ini file at least once before, and can understand what it is much easier than even a simple php file.
The issue of ini files being readable by everyone can be prevented by server side configuration, or even better, by simply adding a single line of code inside a comment line at the top of the file.
That way php will output an 'Direct access forbidden' when the file is accessed via a browser, and the ini file will continue to function as before.
You can use Zend_Config_Ini. It is comfortable and easy. Just simply do not put config files where any user can reach them (for example public_html).
INI files are just one way of dealing with configuration, perhaps the developer came from a Windows-developing background and used whatever he was familiar with :). Besides, PHP offers a convenient way of parsing INI files through the parse_ini_file function.
You'll want to make sure the .INI file is not accessible from the web though. Move it below the docroot so your PHP script can still access it, but random browsers cannot.
For what it's worth, PHP has traditionally used php.ini to configure PHP. So maybe it's some kind of legacy thing?
Seems like this is just former programmer's wish to use different file type for configuration. If there is no other uses for this file, rename it to *.php and forget it. If not, configure webserver to parse ini as php or, better, move it to directory, not reachable from web-server.

Best Practices for locating a function definition in PHP

Is there a simple way to find the file path to where a function is defined? I currently use dreamweavers FIND in an entire directory. Would be nice to have something that doesn't require downloading the entire site tho.
Any suggestions?
Personally I use an IDE like Netbeans or Eclipse PDT. In the case of Netbeans you can ctrl-click on a function and it'll take you to the definition. Sometimes there is a choice in which case it'll make you select one.
But its generally bad form to reuse a function name within your code in different files. It can lead to hard-to-find bugs because it's hard for any program to figure out exactly which one function is actually getting called since source files can be included dynamically.
Would be nice to have something that doesnt require downloading the entire site tho.
I hope this doesn't mean that you're modifying the site remotely.
Have a local working copy, make the changes, test them locally, then upload the changes.
A simple combo of vim and ctags makes the "go to definition" task a piece of cake.
You can't search for something (and expect to find it) unless you have a copy of all the files it might be in.
A number of IDEs have the ability to click and go from a use of a variable or function to its definition. If not that, then a multi-file searching tool within your editor, or something from a command line (such as ack) that is a little more specialised at searching source code can help. Good naming conventions can also help a lot for consistency.
It's not the question, but why don't you have a copy of the site locally - and while you are at it, keep it in version control as well?
I'd sure like this get_functionPath() ability and anyone that has extensively had to work on other people's code would probably find it incredibly useful. We have function_exists, if that could simply return the file the function is defined in for user defined functions it would save a TON of trouble. No, not all of us use IDEs, and yes some of us have been doing this long enough to code on the production machine. Test boxes and sandboxes are for rookies.
One trick is to purposely trigger an error in the function you are trying to locate. Can save a ton of time.
You'd need to use some kind of tool that could build an index on a remote filesystem that you could download and perform local lookup and search upon. I don't know of anything that can do this and a few moments with Google didn't turn up anything.
Maybe a good idea for an open source project? hinthint
so there is no function that would do this? Something like get_class() which would output the parent class but in the case the file path on the server...

Categories