Redefining constant in PHP

wKdnZHv

Advertisements

Take care when you use ($this) in your anonymous functions (closures) – it will be copied

Anonymous functions1 (closures) in PHP 5.3 are cool. Awesome is when you can use2 any variable with it. With PHP 5.4 you can even use $this.

The problem with use is that it is making the value available in the current scope by copying2 it, thus not modifying it upwards the chain. To over come this, simply pass by reference1.

1
$anon = function(){
    // Some fancy execution right here
};
$anon();

2
$otherRandomVar = 5;
$anon = function() use ($otherRandomVar) {
    // $otherRandomVar will be made available in $anon() by copy
    // In this case it is not modifying its value in the global scope
    $otherRandomVar = 8915 + 945;
};

echo $otherRandomVar; // 5

3
$randomVar = 'abc';
$anonFuncRightHere = function() use (&$randomVar) {
    // Passing by reference will solve your issue
    $randomVar = 'cba';
};

echo $randomVar; // cba

How to handle “The plugin generated x characters of unexpected output” errors in WordPress

Some errors are hard to trace, especially when no line of error or function/variable is

If you ever ecounter “The plugin generated x characters of unexpected output” in your WordPress plugin development, a simple solution is to include the following line in your function/method:

file_put_contents(__DIR__ . '/activation_log.txt', ob_get_contents());

This will write any output to a file in the current directory where the file containing the function/method resides.

MAMP fine tuning: enable XDEBUG and turn on error reporting

MAMP addresses developers, so I can’t really understand why these are turned off by default:

  1. Enable XDEBUG:
    • nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini
      (Note: you may be using another version of PHP)
    • Search for “xdebug”
    • Delete the semicolon
    • Restart MAMP (or just Apache)
    • Test with phpinfo(); It should show as compiled
      compiled-xdebug-in-php
  2. Enable PHP Error Reporting: 
    • nano/vim /Applications/MAMP/bin/php/php5.6.10/conf/php.ini – same file as above
      (Note: replace php5.6.10 with your PHP version)
    • Search for “display_errors”
    • Change from “off” to “on”
    • Restart MAMP or just Apache

Happy debugging!

Note to self: Git basics

Some notes on using Git for the future me.

Setup a global config:

git config --global user.name "Gigi Palemier"
git config --global user.email "gigi@palmier.ro"

The Holy Git Figure:

Git File life cycle

 

TODO: branching -vs- tags / advanced workflow
To be updated.

How to clone a Linux server – install same packages onto another server

This small guide is meant for those who need to

  • clone an existing server/desktop the hard way (for those who lack virtualization tools)
  • start from scratch with all packages previously installed (with or without maintaining the personalized settings)

Steps:

  1. Get the current installed packages on the main machine: dpkg –get_selections > installed_packages.txt
  2. Make a copy of your old /etc/apt/sources.list and copy everyting from /etc/apt/sources.list.d/* onto your new machine
  3. Still on the new new machine, make sure you install dselect: apt-get install dselect
  4. Update to check for missing keys: apt-get update
  5. And add the missing keys: apt-key adv –keyserver keyserver.ubuntu.com –recv-keys XXXXXXXX
  6. Update again: apt-get update
  7. Register the new packages: dpkg –set-selections < installed_packages.txt

 

Now the fun part – use dselect to install the missing packages:

  • dselect update
  • dselect install

If you’d like the same personalized settings, simply copy the config files and folders from your /home/user located on the old machine -> onto the new machine. Same rule applies for other programs (apache, fpm, postfix): copy all files from /etc/apache, /etc/postfix and so on onto the new machine. For the future, using rsync or something similar might come more in handy.

This was the ‘hard way’ of cloning a server. Since the best sysadmin is a lazy sysadmin, you might want to consider Chef/Puppet/Ansible/SaltStack or any virtualization & cloning tool.