I have several functions that I wrote and I use regularly on my servers, is there a way I can add them to the core so I don't have to include them from external files?
I am running PHP5
You could add your libraries as a PEAR extension. Then you could add it to your local PEAR repository. Pear is added to the default include path in php.ini. Then you can just use "pear install myextension" on your machines.
If these are C functions you interface with in php (php extensions) then you can do something similar with PECL.
I've done this before.. it's a fairly involved process, but not too bad. This article at zend.com should tell you everything you need to know:
http://devzone.zend.com/303/extension-writing-part-i-introduction-to-php-and-zend/
edit: I should add that there are better ways to achieve the essence of what you're trying to do. Remember that doing this will further clutter up PHP's (already very cluttered) namespace. You're probably better off just making a global include file that has all of your most commonly used functions that you include wherever you need it.
edit2: Upon rereading your original question, you said you don't want to do that, but I still think it's probably the best way. But best of luck to you with the extension route.
If you want your function to always be available, without including it, do this:
Create your function in an php file.
In your php.ini search for the option "auto_prepend_file" and add your php file to that line, like this: auto_prepend_file = "/path/to/my_superglobal_function.php"
Or if you write without a path like this: auto_prepend_file = "my_superglobal_function.php" It will look in your include_path in php.ini to find the file.
Why exactly is it so hard to include the files where you need them?
I suppose the auto_prepend_file PHP.ini directive could work. But it's not really recommended.
If you got autoload, you can move the functions as static methods of a class like My_Functions.
Or for dividing it into more files you can use My_Functions_Math. Then you will only need to load the functions you need. And with autoload you don't have to worry about including files.
You cant autoload namespace functions, so if you want to use autoload the functions have to be static methods in a class. But you can use namespace to make it easier to fx replace the class in the future and/or shorten the long class name. Example:
use My\Functions\Math as Math;
Math::calcThis($i);
Related
I wanted to test the filesystem operations of my cakephp-app using vfsStream, but had to find out that it is seemingly not possible to write files to the virtual filesystem using cakephp`s File and Folder classes. The reason for this seems to be the call to the php function realpath() in the constructor of the Folder-class. vfsStream seems to break when realpath() is used (see http://stubbles.org/categories/5-vfsStream - "realpath() still doesn't work - there is no way to make this work with how realpath() is currently implemented in PHP itself.")
It seems to be that I am out of luck or does any of you know a workaround this issue?
I'm coding a form in Drupal 7, and I want to 'include' a php file that adds my DB connection variables to my code.
I've tested my code in several ways, and I'm sure that the 'include' makes me get a WSOD when I run Cron in my site.
I've Googled about this and I tried:
include("/sites/all/libraries/php/connection.inc");
include("./sites/all/libraries/php/connection.inc");
include"./sites/all/libraries/php/connection.inc";
I also tried the above with '.php' extension.
And my last try:
define('__ROOT__', dirname(dirname(__FILE__)));
include(__ROOT__.'/sites/all/libraries/php/connection.inc');
Sometimes I get a WSOD when trying to run Cron, and sometimes my page styles get broken when I submit a form.
What is the correct way to include a PHP file manually in Drupal? Note that I don't want to use the 'Drupal way' to do this or to use the webform module. I want to code it manually with PHP.
The libraries directory should only be used to store files shipped as a library through the libraries module (see here https://drupal.org/project/libraries).
For both examples lets assume library.inc is our file and relative_path_to is the relative path based on the module directory to our library.inc file.
To just include a file you can use:
require(drupal_get_path('module', 'module_name') . '/relative_path_to/library.inc');
And to do it the Drupal way (https://api.drupal.org/api/drupal/includes!module.inc/function/module_load_include/7):
module_load_include('inc', 'module_name', '/relative_path_to/library');
Cheers,
j
Try this ways
1) require 'includes/iso.inc';
2) include_once 'includes/iso.inc';
3) include ('includes/iso.inc');
OR Drupal Ways
include_once DRUPAL_ROOT . '/includes/iso.inc';
In my opinion these are correct Drupal (7) ways to include code:
module_load_include(); function - Drupal API documentation - to include any PHP files from within your or other modules where needed,
files[] = ... in your_module.info file to autoload include classes and interfaces within your own module - Writing .info file documentation.
Libraries module provides it's own way to include external PHP files from the libraries.
The second answer is the correct "Drupal way". The one that was accepted as the answer has many potential pitfalls if locations of things change. Using drupal_get_path is the best way. This is also especially true if you are trying to include a file that may not be fully bootstrapped during a module update hook.
Hi this is my first post on here. I am trying to install the Gdata Zend Client library without much success.
I have used these resources + scoured Stack Overflow.
https://developers.google.com/gdata/articles/php_client_lib
http://jeromejaglale.com/doc/php/google_calendar_api
I want to be able to add,edit etc events on google calendar via PHP. My problem/question is i really dont understand what the include_path settings are all about and how to set them in order to make the class work. Of course i checked php manual regarding this but still draw a blank.
I have downloaded the relevant class and uploaded it to my web root. In the past i would just include a class by using php include at the top of the page and this would suffice.
I am of the understanding that i need to change the php.ini file to show php where my class is. Does this mean that i have to put my class somewhere else other than the web root.
I am terribly confused about this step and i know that if i can get it installed, actually using the class should be relatively easy.
Thanks for any help.
Welcome! Your Q is about include files rather than ZF elements.
Understanding where the include setting can be made (and subsequently overridden) is an absolute key bit of information.
You need to find out where it is on the server you are working on.
echo ini_get('include_path');
Then dash off and really, really read the corresponding manual page.
Try out including a very simple file with an echo statement, and you will regain your sanity and confidence.
http://www.php.net/manual/en/function.include.php
Get that working then have a play with this:
http://www.php.net/manual/en/function.ini-set.php
The experience how you can include a file from the same directory (not generally a good idea if that is a public webpage - inside your webroot)
Then if you want to really chase this thing down, look at where you can set this in Apache and per-directory in .htaccess files.
Finally, you can just include a file by telling include/require the exact path from the top of the tree;
include /var/www/includes/libraries/and/so/on.php;
There are SO many places you can set and override this that you are really best off finding out where the server thinks the include directory is and putting your compoenents in there:
Now, when that comes to ZF stuff, I (on Deb and Ubuntu anyhow) put the contents of
Zend Framworks version XYZ ZendFramework/lib/Zend <-that folder into:
/usr/share/php/Zend <-into this place
Then setup your autoloader and Robert is your mothers brother...
Zend/Gdata.php line 124 is like this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’)
Change that to this:
public static function import($uri, $client = null,$className=’Zend_Gdata_Feed’, $useObjectMapping = true)
I have a sharedlib.cc and a sharedlib.h code that contains classes and methods. I wrap this up and created a shared library called MySharedLibrary.so.
I would like to work with my .so library inside a php code. So I've created a config.m4 file , a php_code.cc file and a php_code.h file in order to make the extension from php possible. I DON'T WANT TO include in the config.m4 file the sharedlib.cc. I would like to include instead the MySharedLib.so because I don't want to expose my sharedlib.cc code. Besides I have others library that I also included in my .so library. Need some help.
EDIT:!!
I DON;T WANT TO INCLUDE IN THE CONFIG.M4 THE FILE: sharedlib.cc. I want to include only the sharedlib.h and MySharedLibrary.so (the lib that contains both the sharedlib.cc and sharedlib.h)
I believe the PHP_ADD_LIBRARY() macro in config.m4 will achieve this.
You can find some examples here: http://devzone.zend.com/article/4486
edit: you may need PHP_ADD_LIBRARY_WITH_PATH() if your shared lib isn't somewhere that the linker will find it.
edit 2: looks like this was covered before; the answer provides good example code.
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