I'm trying to get PHP autocompletion right in Vim. Right now when I do a $blog = new Blog(); $blog-> and then hit CTRL+X CTRL+O I'd expect omnicompletion to return all the functions in the class Blog.
Instead, it returns all functions for the entire project. I've built ctags for my project like so: ctags -R *
Is there any way to make the autocompletion context-aware?
catchmeifyoutry's answer points out a work-around by adding a comment such as /* #var $myVar myClass */ immediately before the line on which you use omnicomplete, however this is cumbersome and for the time it takes to write the comment, you may as well have written the function name yourself.
Solution: phpComplete
It is a Vim script: phpComplete
You will still need a tags file generated for your classes, but you can then use omni complete within the file, like so (modified from the description on the script's page);
This patch allows for in-file checking so you don't need the comment.
$blog = new Blog;
...
$blog->Blah(); // <-- complete without comment
It also allows support for singleton instantiations:
$instance = Class::getInstance();
$instance->completeMe(); // sweet completion
" Assuming Vim 7 (full version) is installed,
" adding the following to your ~/.vimrc should work.
filetype plugin on
au FileType php set omnifunc=phpcomplete#CompletePHP
" You might also find this useful
" PHP Generated Code Highlights (HTML & SQL)
let php_sql_query=1
let php_htmlInStrings=1
" Hope this helps!
(via http://www.linuxquestions.org/questions/linux-software-2/vim-omin-completion-for-php-621940/#post3155311)
Omnicompletion will only work if the tag file contains both the class definition, and the variable declaration.
Straightforward solution
In general that means that you will need to save and (re)generate the tags file after the class Blog { ... } and $blog = new Blog(); parts, but before trying $blog-> <C-X><C-O>.
This is because the PHP omni-complete function will look for the class declaration of the $blog variable in the tags file.
(BTW if you have opened the tags file in a buffer, reload it after regenerating it.)
Alternative
The vim documentation (:help ft-php-omni) also defines a way which doesn't require the variable to be indexed in the tags file, but uses instead a specific comment on the preceding line:
/* #var $myVar myClass */
$myVar->
However, the class definition still does need to be in the tag file, and the comment is needed every time you want to use omni-complete. So typing away in a new PHP file still won't give you nice omni-completion :(
Just a thought
Maybe it is possible to generate on-the-fly a temporary tags file (like the taglist plugin) of just the unsaved buffer, and allow omni-complete to use it too?? I'm not a great vim hacker though...
This one works as expected:
https://github.com/shawncplus/phpcomplete.vim
I am just missing the function parameters in the pveview!
The following works better. Taken from http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html
ctags \
-f ~/.vim/tags \
-h ".php" -R \
--exclude="\.svn" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+ivcf \
--regex-PHP='/(abstract)?\s+class\s+([^ ]+)/\2/c/' \
--regex-PHP='/(static|abstract|public|protected|private)\s+function\s+(\&\s+)?([^ (]+)/\3/f/' \
--regex-PHP='/interface\s+([^ ]+)/\1/i/' \
--regex-PHP='/\$([a-zA-Z_][a-zA-Z0-9_]*)/\1/v/' \
Even with the above, there seems to be some issues. e.g. phpcomplete doesn't seem to support methods of instance vars.
$this->objA = new SomeClass();
$this->objA-><do_autocomplete> #fails
However,
$objA = new SomeClass();
$objA-><do_autocomplete> #works
After trying to get phpcomplete working for the last few hours my advice to anyone also trying, is to STOP. It doesn't work well and is not worth the trouble.
In C++, I run the following to get better context sensitivity:
ctags '--c++-kinds=+p' '--fields=+iaS' '--extra=+q'
It's not perfect, but after ctags adds the extra information to the tags file as specified by the above command vim handles completion better.
You can use a pretty powerful combo:
Phpactor
nvim-completion-manager
I tried a lot of stuff: PHPComplete, Padawan and so on. This is the best I could find.
In case you are interested, I wrote as well an article how to do a PHP IDE with Vim.
I've created a vim plugin for my padawan.php completion server. Checkout this video to see how it works.
try
curl -L -s https://git.io/ide | sh
then relaunch your nvim. You might got code completion, and goto definition features.
*Currently, it's only available for neovim
Related
I'm trying to get some "repl-like" feature for PHP, inside vim.
Basically, what I want is to be able to visually select a part of my script, execute it, and see the result in a separate buffer.
But I don't want to execute the whole current file (so :!php % doesn't do the trick ...)
I found the vim-quickrun plugin, which seems to greatly fit that need, but can't make it work and when looking for more documentation, most of the result I get are in japanese (I don't speak japanese :( ... )
For now, I have installed the plugin via Vundle, but have not added any extra configuration to my .vimrc
From inside a file, I can type
...
echo 'hello quickrun sh test'
...
=> visual select the date line, and type
:QuickRun sh
I got my hello world printed, all fine
But if I do
...
echo 'hellow quickrun php'
...
=> visual select ...
:QuickRun php
I just get a buffer with just the same text that I typed, no execution ...
Does someone already achieved something like this ?
Thanks a lot !
EDIT :
PHP is correctly added to my PATH. Added the 2 config lines suggested below ... Sadly, it doesn't change anything :(
You need to put the php flags around your php code, like any php script (it always starts in plain text mode):
...
<?php
echo 'hellow quickrun php';
?>
....
Then you can select only one part with QuickRun, but don't forget to select the flags as well.
I don't use that plugin, but I think you need to configure something like this in your ~/.vimrc:
let g:quickrun_config = {}
let g:quickrun_config.php = {'command' : 'php'}
and have the php executable in your PATH.
The following solution does not use vim-quickrun but allows you to visually select, execute and see the result just as you like. You need vim-slime with phpsh :
First, install the vim-slime plugin. It allows to send lines and visually selected chunks of code from VIM to a screen or tmux session.
Now install screen: On Ubuntu, do sudo apt-get install screen.
Open a terminal and start screen with a session name: screen -S sessionname.
Open a second terminal and start vim. Write some code, visually select it and press <C-c><C-c>, that is two times CTRL+C. You will be asked for the session name, use sessionname as before. The selected lines will be sent to the first terminal just as if you had written them directly there.
To make use of this functionality, you need to start an interactive PHP shell in the first terminal, such as phpsh.
Good evening,
I am trying to figure out a way to display simple node/edge graphs from a Neo4J DB in PHP. I had a look at three.js, sigma and alchemy but those tools seem way to complex for such simple tasks. Then I found GraphViz which runs fine when I use the editor. But I was not able to find help when I wanted to use this from PHP. There is a pear package which seems not suitable anymore for PHP >5.0 (tried it and run into lots of errors (e.g. Non-static method System::mktemp() should not be called statically - there is also a thread here about this: Graphviz not working with php 5.3.2) and I found a document http://www.graphviz.org/pdf/gv.3php.pdf - but unfortunately it seems I cant deal with this.
Anybody has an example how to draw e.g. this:
<?php
require_once 'Image/GraphViz.php';
$gv = new Image_GraphViz();
$gv->addEdge(array('wake up' => 'visit bathroom'));
$gv->addEdge(array('visit bathroom' => 'make coffee'));
$gv->image();
?>
(Source:http://pear.php.net/manual/en/package.images.image-graphviz.example.php)
This code is from the pear package example but doesnt run - but maybe someone has an idea how to use this from PHP directly over the gv.php extension from GraphViz?
A GraphViz Example which runs fine in the editor:
graph graphname {
a -- b;
b -- c;
b -- d;
d -- a;
}
But how to get PHP to talk with GraphViz and produce the output?
Any help is very appreciated, thanks.
B
You can also just use a javascript based dot renderer, e.g.
https://github.com/mdaines/viz.js/
https://code.google.com/p/canviz/
Result of my search:
I am using now http://visjs.org/ which suits good to my needs. Here is an example for the code: http://visjs.org/docs/network.html#Example
The problem to assign URLs to nodes has been solved by altering the javascript with a simple location.href on click.
They gave me some tasks over a huge site realized with Magento.
I use Netbeans with Xdebug to debug (I'm on lubuntu oneiric btw) and I'm finding myself quite fine with all my tasks.
It happened to me to face a task concerning a class redeclaration and I lost a lot of time in finding the right file to change.
Just to learn how to do it correctly:
the task was to change the meta keywords, starting from a code like
<meta name="keywords" content="<?php echo htmlspecialchars($this->getKeywords()) ?>" />
With Netbeans debugger I can easily open the file containing getKeywords() which has:
public function getKeywords()
{
if (empty($this->_data['keywords'])) {
$this->_data['keywords'] = Mage::getStoreConfig('design/head/default_keywords');
}
return $this->_data['keywords'];
}
Now I couldn't find an easy way to go on through debugging in both cases :(
In particular my case is that $this->_data['keywords'] is not empty on the page I had to correct ..
so how can I easily know how this object is created and in particular who fills _data['keywords']?
It took me long time to find the right file by myself.
I'm not so expert in debugging, so maybe I could do it with netbeans w/ xdebug, but I cannot figure it out.
Thanks
open up terminal and
grep 'setKeywords(' app/code/ -rsn
this will reveal you the locations where this variable is set or used
grep -ri "Keywords" * | grep -v cache
'-r' means recursive
'-i' means case insensitive
'*' means any file name
'| grep -v cache' means strip out any references into the cache directory
You could temporarily create the method:
public function setKeywords($s) {
echo sprintf('<pre>%s</pre>', print_r(debug_backtrace(), true));
exit;
}
Add this code to same class that has getKeywords() so that when someone adds data via this function, you will see the backtrace and figure out how it happened
I have to develop a pretty simple php website so I don't need framework.
But it's must support multi language (EN/FR/CHINESE).
I have looked for php built in system and I found two ways :
intl module from php5.3 (http://php.net/manual/fr/book.intl.php)
gettext (http://php.net/manual/fr/book.gettext.php)
I have no experience in i18n without framework, so any advices about what's the simplest way to support multi language ?
At end I just need a function that search translation into file (one file by language).
EQ :
trans('hello');
=> en.yaml (yaml or not, it's an example)
hello: "Hello world!"
=> fr.yaml
hello: "Bonjour tout le monde !"
And if possible I prefer Pure PHP implementations
Although ext/gettext and ext/intl are both related to i18 (internationalization), gettext deals with translation while intl deals with internationalizing things like number and date display, sorting orders and transliteration. So you'd actually need both for a complete i18-solution. Depending on your needs you may come up with an home-brew solution relying on the extensions mentioned above or your use components provided by some framework:
Translation
Symfony 2 Translation component: https://github.com/symfony/Translation
Zend Framework Zend_Translate
Internationalization
Zend Framework Zend_Locale
If you only need translation and the site is simple enough, perhaps your simple solution (reading a translation configuration file into an PHP array, using a simple function to retrieve a token) might be the easiest.
The most simple solution I can think of is:
$translation = array(
'Hello world!' => array(
'fr' => 'Bonjour tout le monde!',
'de' => 'Hallo Welt!'
)
);
if (!function_exists('gettext')) {
function _($token, $lang = null) {
global $translation;
if ( empty($lang)
|| !array_key_exists($token, $translation)
|| !array_key_exists($lang, $translation[$token])
) {
return $token;
} else {
return $translation[$token][$lang];
}
}
}
echo _('Hello World!');
I know this is an old question, but I feel that the answers are lacking a more hands-on approach from start to finish. This is what I did to get translation working using PHP's gettext library and Poedit without using any additional PHP libraries on a Debian server:
Preparation step 1: Install gettext and the locales on the server
I am not sure how this is done with other operating systems, but for Debian, you do:
sudo apt-get install gettext
sudo dpkg-reconfigure locales
Edit: I assumed Ubuntu would be the same as Debian, but apparently it's slightly different. See this page for instructions for installing locales on Ubuntu.
Make sure you select all of the locales that you want to use. You should then see something like:
Generating locales (this might take a while)...
en_US.UTF-8... done
es_MX.UTF-8... done
fr_FR.UTF-8... done
zh_CN.UTF-8... done
Generation complete.
Note: Make sure you select the right variants and character encodings (most likely UTF-8) for each language. If you install es_MX.UTF-8 and try to use es_ES.UTF-8 or es_MX.ISO-8859-1 it won't work.
Preparation step 2: Install Poedit on the translators' computers
Poedit is available from the software repository for most Linux operating systems. For Debian-based, just execute:
sudo apt-get install poedit
For Windows and Mac, go to: https://poedit.net/download
Start coding:
Ok, now you're ready to get started coding. I wrote the following gettext() wrapper function to translate both singular and plurals:
function __($text, $plural=null, $number=null) {
if (!isset($plural)) {
return _($text);
}
return ngettext($text, $plural, $number);
}
Example usage:
// Singular
echo __('Hello world');
// Plural
$exp = 3;
printf(
__(
'Your account will expire in %d day',
'Your account will expire in %d days',
$exp
),
$exp
);
This will work for all languages, not only languages where plural is anything where n != 1 - this includes languages with multiple plural types.
You can also add translator notes like this:
/** NOTE: The name Coconut Hotel is a brand name and shouldn't be
translated.
*/
echo __('Welcome to Coconut Hotel');
You can change the text from NOTE to whatever you want, but you will have to alter it in the shell script below. Important: The translators note must be part of a comment on the line immediately preceding the __() function or it won't be picked up when we scan the PHP files for translatable strings.
// Warning! THIS WILL NOT WORK!
/* NOTE: This translator's note will not be picked up because it is
not immediately preceding the __() function. */
printf(
__(
'Your account will expire in %d day',
'Your account will expire in %d days',
$exp
),
$exp
);
// Warning! THIS WILL NOT WORK!
After you are ready to send the strings off to the translators, save the following as a shell script (e.g. update.sh) in your application's root directory:
#!/bin/sh
find . -iname "*.php" | xargs xgettext --add-comments=NOTE --keyword=__:1,2 --keyword=__ --from-code=UTF-8 -o i18n.pot
find . -name '*.po' | xargs -I{} msgmerge -U {} i18n.pot
To execute it, just do:
cd /path/to/script && sh update.sh
This will recursively scan for all PHP files in that directory and create a .pot file (I called it i18n.pot, but feel free to name it whatever you like) and update any existing .po files it finds with the new strings.
We then need to create the directories that all the locale files will be stored, one for each locale. They need to be of the format ./locale/{locale}/LC_MESSAGES. For example:
cd /path/to/your/project
mkdir -p ./locale/en_US.UTF-8/LC_MESSAGES
mkdir -p ./locale/es_MX.UTF-8/LC_MESSAGES
# ...etc.
You need to decide on a text domain to use. This can be anything you want, but the script will look for a file called {yourTextDomain}.mo within the LC_MESSAGES folder for that language. Put the following in your PHP script:
define('TEXT_DOMAIN', 'yourdomain');
bindtextdomain(TEXT_DOMAIN, __DIR__.'/locale');
textdomain(TEXT_DOMAIN);
bind_textdomain_codeset(TEXT_DOMAIN, 'UTF-8');
Then to actually switch to another locale, do:
$lang = 'es_MX.UTF-8'; // Change this to the language you want to use
if (setlocale(LC_ALL, $lang) === false) {
throw new Exception("Server error: The $lang locale is not installed. Please update the server's localisations.");
}
putenv('LC_ALL='.$lang);
Initially, you send the .pot file generated by the script above to the translators. They then open Poedit and click on File > New from POT/PO file. When they save it, they need to save it as {yourTextDomain}.po. The {yourTextDomain} needs to be exactly the same as the text domain you have in your PHP script. When they save it, it will automatically create both the .po file and the .mo file. Both of these need to be saved in that language's LC_MESSAGES directory when they are done translating.
Now when you update the strings in your PHP file, just re-execute the shell script and send the newly updated .po files to the translators. They then translate the strings and both the .po and .mo files need to be re-uploaded.
That's it. It may seem slightly difficult to get set up, but once you have it up and running, it's really easy.
Gettext seems to be what you need.
There is a file by langage (except for the original one) and it's very easy to use :
echo _('Bonjour, ça va ?');
will print Hello , how are you ? in english.
There is some tools with gettext that could scan your php file and search for translatable string (in fact all string in _() or gettext()). Thanks to that you don't have to worry about the different langage file. You just code your website in the original langage and the langage file will automatically created later.
Nevertheless gettext is more a translation tools whereas intl is really an i18n one (number formating for example)
Althought you don't need a framework you can use a framework. The internationalization features in Zend Framework is pretty good and you can just use that part of it instead of using all the parts (including MVC)
I have a script with alot of nested includes and functions calling each other from lots of if conditions. Basically, its a coding nightmare.
Is there any way i can "PRINT" the PHP code executed ? I mean, print the actual flow of the code and the path taken by the script from start to end ?
PHP can't do this out of the box. You'd need to install the xDebug extension on your PHP development machine. Once installed, you could use the code coverage function to determine which lines have executed.
Lacking that, I'd create a simple debug function to include at the top of your code
public function myDebugString($string)
{
file_put_contents('/tmp/debug.log',"$string\n",FILE_APPEND);
return;
}
and then add calls to this throughout you code
myDebugString('Called at ' . __LINE__);
And then tail the log file created. Removing the debug statements is a simple find/replace operation for your editor once you're done.
Many frameworks have debugging objects that do way more than this built it, but if you're dealing with stand alone code something simple like this should be enough to get you by.
You can try debug_backtrace() or debug_print_backtrace().
Additionally, I recommend using Xdebug. It prints a very useful stack trace on exceptions (you can configure it to print out every method parameter and every local variable (xdebug.collect_params=4 and xdebug.show_local_vars=on configuration parameters).
Take a look at code coverage tools. This allows you to identify those functions and lines of code that are actually executed when a script runs