Eclipse - PHP Include Path - php

I´m working with Eclipse 3.4.2 with PDT.
I´ve added some libraries in the applications folder, and add that folder to the PHP Include Path.
When I run as script, it works perfect, but if I access the page outside eclipse, the libraries are not accesible, i need to add this line:
set_include_path(
implode(PATH_SEPARATOR, array(realpath('../application'), get_include_path(),))
);
Is this necesary? how can avoid this?

You may also specify path in include() statement, but it's not very convenient.
Also, you may specify corresponding include_path value in php.ini configuration file (see here for details), but usually this directive contains path to system-wide libraries, not application-specific paths.
If you are using OOP, you may implement your own class-loader, which will look for classes in specific directories. See this article for details.

Yes. It is necessary. Because PHP interpreter should know, from what folders load libraries.

Related

Where to place Zend and which path to change?

I downloaded and extracted Zend Framework and it is inside a folder named ZendFramework-2.0.6.
Documentation says that I need to change php.ini - include_path line.
I hope - this is that line (unchanged): include_path = ".;C:\xampp\php\PEAR"
On this page, accepted answer says that i shouldn't change this path (if I understand well).
Could someone clarify where I should place ZendFramework-2.0.6 folder, which path and how I need to change it ?
I'm using xampp 1.8.1.
It's completely up to you but it is a common practice to place it into the vendor directory.
However, I recommend using composer for installation as it automatically installs dependencies and configures your autoloading. It's surely a good idea to start with the Skeleton Application, the README.md explains the installation process with composer.
You don't need to change the include_path in this case.

No include_paths are working for Zend

I'm running out of ideas here. Physically, my Zend install (XAMPP on Windows) is located at D:\xampp\htdocs\newsite\zend\library.
So far, I've managed to fail with all of the following include paths, as reported in zend.php (located at D:\xampp\htdocs\newsite and accessed from 127.0.0.1/newsite/zend.php.
.;D:\xampp\php\PEAR
.;D:\xampp\php\PEAR;/zend/library
.;D:\xampp\php\PEAR;D:\xampp\htdocs\newsite\zend\library
.;D:\xampp\php\PEAR;D:/xampp/htdocs/newsite/zend/library
.;D:/xampp/htdocs/newsite/zend/library
.;D:\xampp\htdocs\newsite\zend\library
D:\xampp\htdocs\newsite\zend\library
.;./zend/library;D:\xampp\php\PEAR;D:\xampp\htdocs\newsite\zend\library
.;./zend/library;D:\xampp\php\PEAR;D:\xampp\htdocs\newsite\zend\library;/zend/library
.;./zend/library;D:\xampp\php\PEAR;D:\xampp\htdocs\newsite\zend\library;D:/xampp/htdocs/newsite/zend/library
EDIT: Yes, I got all of the above through get_include_path(). I'm on PHP 5.3.8.
Assuming you are not trying to use auto loading, which would be a good idea to rule out problems with auto loaders.
Since the include_path returns ".;D:\xampp\php\PEAR;D:\xampp\htdocs\newsite\zend\library" I would expect a simple include to work.
Assuming you are using ZF2, this test should not error, if the above include path is being returned:
<?php
include ('Zend\Version\Version.php');
If that does error, try it with a full path to verify if it's the include path or something else that is causing the file not to be included.
<?php
include ('D:\xampp\htdocs\newsite\zend\library\Zend\Version\Version.php');
If your test is more complicated that the above, maybe it's something other than the include path going wrong. Can you post the code that is failing?
Also, you might consider installing the ZF2 Skeleton App, which shows current best practices for setting up autoloading, namespaces, etc.
https://github.com/zendframework/ZendSkeletonApplication
While using Zend Library, make sure
1) You have parent dir of Zend folder in include paths
D:\xampp\htdocs\newsite\zend\library has to be in include paths, not D:\xampp\htdocs\newsite\zend\library\Zend
2) PHP not running in safe mode, also PHP is updated to 5.2+ or 5.3+ and if apc is turned on, it must have apc.include_once_override off.
If still not working, inspect by printing get_include_path() or phpinfo();
That should be it.

Is Zend Framework standalone or part of PHP environment?

Should ZF and PHP have the same path to include in php.ini, or can they be put in separate directories? This is the include path in my php.ini:
;Windows: "\path1;\path2"
include_path = ".;C:\xampp\php\PEAR;C:\ZF\library\"
I couldn't find any Zend documents that specify which, copying ZF\library to xampp\php\PEAR or the entire ZF directory, or using multiple include paths, is recommended for MVC app production. Is there a good practice or does it depend more on not having access to a certain part of a server or if you are using shared hosting, combining PHP and ZF would be the better option?
Should ZF and PHP have the same path to include in php.ini, or can
they be put in separate directories?
It is fine to have multiple directories listed in your php.ini.
include_path = ".;C:\xampp\php\PEAR;C:\ZF\library\"
The following would be better:
include_path = "C:\xampp\php\PEAR;C:\ZF\library\"
In other words, you don't need the "." in your path since anything that needs to be included in the current working directory can be included like this "./path/to/file.php".
Furthermore, you will want to use an autoloader but having the autoloader search ".", is more work than is needed.
Finally, I generally have an include path of:
include_path = "C:\xampp\php\PEAR"
The reason is that since PHPUnit is generally installed via the PEAR installer, I always want that globally available; however, for everything else, I can wait until I have access to a PSR-0 compliant autoloader.
Hope that helps.

How to redefine a function in php?

Now i'm stuck with file_exists() function. My project is built from repositories, one is symlinked as a DocRoot, others are available to php interpreter via include_path. But the default file_exists() cannot look at the files are placed somewhere under include_path, so i need or somehow redefine this func with a wrapper or to write my own, include it in other modules and replace all matches of this func in the directory tree. It's possible, but is not good and is not clear for developers.
I've already seen pecl-apd (2008) and pecl-runkit (2007), but there are no more such pecls among the others in my gentoo repository or the overlays. So, how to redefine a function in a modern way?
== upd ===
Ok. Let's go deeper.
Assume we have /home/me/repos/ directory, where our git modules are placed and a virtual host /var/srv/domain.com, where it all is assembled
repos_
\_site_root_
\_.git
\_file1
\_module1_
\_.git
\_we_need_this_file_too
\_module2_
\_.git
domain.com_
\_htdocs –> ~/repos/site_root
\_tmp
so we need to somehow include folders with modules 1 and 2 into the project. Then adding path to ~/repos to php include_path via vhost config
php_admin_value include_path ".:/home/me/repos"
Now php can
include ('module1/we_need_this_file_too');
from my repos directory. but when it tries to check the file before including with file_exists(), it fails, because those directories are still closed for file_exists(). This is the problem.
Now i've found a workaround with absolute paths, anyway i'd have to create a personal config in my project. I also though about to chdir() into /home/me/repos/ when it needs to (and describing <Directory> section in vhost), but it was bad idea.
From the PHP manual
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
Well, you may, as you already mentioned, if you install the runkit extension and use runkit_function_redefine or if you install APD and use override_function.
I working on library for function redefining in php5.3
Source: https://github.com/Bubujka/bu.defun
Exampls: http://defun.bubujka.org/doc.html
Simple redefining:
<?php
def('name', function(){
echo "Waserd";
});
name();
echo "\n";
def('name', function(){
echo "Bubujka";
});
name();
---
Waserd
Bubujka

Robust Directory Manager in PHP

This is a general question. What are some techniques you employ to prevent the links in your PHP code from breaking every time you move a file or move a file into a different directory?
For front end stuff, always use absolute URLs (start with '/' so you can move from domain to domain as needed).
For internal include() / require() type stuff, do as Gaurav suggests and use a config file that creates a constant to represent your path (so you can change it easily as needed from one location in your code).
For library type stuff (i.e. classes, functions, etc) that you want to reuse, I would add that to the include_path either via php.ini (global or local), .htaccess (if you are using apache) or via the ini_set() function. That way you can include these files by just the filename (i.e. <?php include_once('library.php'); ?>)
If you go the ini_set route, take a look at the auto_append directive (which in turn can be set via php.ini, .htaccess or ini_set)... that way you can have a 'bootstrap' file added to every page request that sets up your include_path for just that application without having to add the ini_set statement every where you turn.
With all that said, I recommend that you:
think your application layout ahead in advance, develop a common convention, and stick to it.
consider learning about design patterns (MVC, et al), which will get you thinking in new ways about how you design your applications
adopt the use of an application framework (CakePHP, Zend Framework, etc) which will come with a suggested (or mandated) file/directory layout and keep you from having to manage file locations and stuff.
Good luck!
If you move/rename a file that is linked throughout your web page and would like to ensure that links still work, I would write a 303 redirect (where and how depends on your web server and it's configuration).
I use a configuration file wherein I define all the paths (using define()) to various directories like 'webroot', 'application', or 'css'
This way I need to change only one line in one file and the changes are affected in all files wherein this variable is used.

Categories