At production level session_regenerate_id warning is annoying. So I suppress ZF2 (2.4.13) session manager session_regenerate_id warning by changing Zend/Session/SessionManager.php file’s regenerateId method by putting “@” in front of session_regenerate_id function.
Category: php

Changing ZF2 core 2.4.13 \Zend\Stdlib\Hydrator\AbstractHydrator::hasStrategy
class: Zend\Stdlib\Hydrator\AbstractHydrator
function: hasStrategy
php version: 7.4
public function hasStrategy($name)
new change / new code
{
if(is_array($this->strategies))
return array_key_exists($name, $this->strategies)
|| array_key_exists(‘*’, $this->strategies);
if(is_object($this->strategies)) return property_exists($this->strategies, $name);
return false;
}
Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /media/shkr-home-wifi/works/workspace/vendor/ZF2/library/ Zend\Stdlib\Hydrator\AbstractHydrator on line 87
reason of change
public function hasStrategy($name)
previous code
{
return array_key_exists($name, $this->strategies)
|| array_key_exists(‘*’, $this->strategies);
}
Changing ZF2 Core 2.4.13 /Zend/I18n/Translator/Loader/Gettext::load
class: Zend\I18n/Translator/Loader/Gettext
function: load
php version: 7.4
Last portion change …….
if ($textDomain->offsetExists(”)) {
$rawHeaders = explode(“\n”, trim($textDomain->offsetGet(”)));
foreach ($rawHeaders as $rawHeader) {
list($header, $content) = explode(‘:’, $rawHeader, 2);
if (trim(strtolower($header)) === ‘plural-forms’) {
$textDomain->setPluralRule(PluralRule::fromString($content));
}
}
$textDomain->offsetUnset(”);
}
………….. end of change ………………..
Reason to change: Deprecated: array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in …/Zend/I18n/Translator/Loader/Gettext.php on line 142
///////// previous code ///////////////////
if (array_key_exists(”, $textDomain)) {
$rawHeaders = explode(“\n”, trim($textDomain[”]));
foreach ($rawHeaders as $rawHeader) {
list($header, $content) = explode(‘:’, $rawHeader, 2);
if (trim(strtolower($header)) === ‘plural-forms’) {
$textDomain->setPluralRule(PluralRule::fromString($content));
}
}
unset($textDomain[”]);
}
Changing ZF2 Core 2.4.13 Zend\View\Helper\HeadLink::createDataStylesheet
class: Zend\View\Helper\HeadLink
function: createDataStylesheet
php version: 7.3
public function createDataStylesheet(array $args)
new/modified/final/current code
{
$rel = ‘stylesheet’;
$type = ‘text/css’;
$media = ‘screen’;
$conditionalStylesheet = false;
$href = array_shift($args);
if ($this->isDuplicateStylesheet($href)) { return false; } if (0 < count($args)) { $media = array_shift($args); if (is_array($media)) { $media = implode(',', $media); } else { $media = (string) $media; } } if (0 < count($args)) { $conditionalStylesheet = array_shift($args); if (!empty($conditionalStylesheet) && is_string($conditionalStylesheet)) { $conditionalStylesheet = (string) $conditionalStylesheet; } else { $conditionalStylesheet = null; } } $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet'); if (0 < count($args) && is_array($args[0])) { $extras = array_shift($args); $attributes['extras'] = (array) $extras; } return $this->createData($attributes); }
public function createDataStylesheet(array $args)
previous/original/old code
{
$rel = ‘stylesheet’;
$type = ‘text/css’;
$media = ‘screen’;
$conditionalStylesheet = false;
$href = array_shift($args);
if ($this->isDuplicateStylesheet($href)) { return false; } if (0 < count($args)) { $media = array_shift($args); if (is_array($media)) { $media = implode(',', $media); } else { $media = (string) $media; } } if (0 < count($args)) { $conditionalStylesheet = array_shift($args); if (!empty($conditionalStylesheet) && is_string($conditionalStylesheet)) { $conditionalStylesheet = (string) $conditionalStylesheet; } else { $conditionalStylesheet = null; } } if (0 < count($args) && is_array($args[0])) { $extras = array_shift($args); $extras = (array) $extras; } $attributes = compact('rel', 'type', 'href', 'media', 'conditionalStylesheet', 'extras'); return $this->createData($attributes); }
Install php 7.2 ssh2 in ubuntu 16x
recently i need to install ssh2 connection through my php code to connect remote server by sftp for file transfer. i’m using php 7.2 and face a “sigment fault” issue for normal installation. i solved this issue by install required demon. hope it may help someone who face same issue.
first you need to install/upgrade some basic program
apt-get install gcc make autoconf libc-dev pkg-config
then install base library
apt-get install libssh2-1-dev
now install required php modules
apt-get install php7.2-dev php-pear
now install ssh by pecl (the most important part of installation)
— pecl channel-update pecl.php.net
ignore commented line OR use if you understand by yourself
pear config-show
— pear config-set php_ini /etc/php/7.2/apache2/php.ini
— pear config-set temp_dir /etc/php/temp/pear
pecl install ssh2-1.1.2
now you need to enable ssh2 extension into your php cli installation
echo “extension=ssh2.so” > /etc/php/7.2/mods-available/ssh2.ini
please check you php installation/configuration path. set priority on your own. i set here 30 without proper understanding đ
ln -s /etc/php/7.2/mods-available/ssh2.ini /etc/php/7.2/cli/conf.d/30-ssh2.ini
now another important part is your PHP code. when we use ssh2 in fopen wraper, in other version of ssh2 connection we need to open a connection and we can use resource id. but with the above change you must use user, password, port i.e. full access information every time we need to connect to server. here is my sample code –
$fh = @fopen(‘ssh2.sftp://’ . $this->user.’:’.$this->pass.’@’.$this->ip.’:’.(intval($this->port)>0?intval($this->port):22) . $pRemoteLocation, $pMode);
all other code like directory creation or any other command execution may be same as before.
changing zf2 core 2.4.13 Zend/Stdlib/ArrayObject.php
class : Zend\Stdlib\ArrayObject
function: unserialize($data)
php version: 7.3
previous (original) code
/**
* Unserialize an ArrayObject
*
* @param string $data
* @return void
*/
public function unserialize($data)
{
$ar = unserialize($data);
$this->protectedProperties = array_keys(get_object_vars($this));$this->setFlags($ar[‘flag’]);
$this->exchangeArray($ar[‘storage’]);
$this->setIteratorClass($ar[‘iteratorClass’]);foreach ($ar as $k => $v) {
switch ($k) {
case ‘flag’:
$this->setFlags($v);
break;
case ‘storage’:
$this->exchangeArray($v);
break;
case ‘iteratorClass’:
$this->setIteratorClass($v);
break;
case ‘protectedProperties’:
continue;
default:
$this->__set($k, $v);
}
}
}
new code
public function unserialize($data)
{
$ar = unserialize($data);
$this->protectedProperties = array_keys(get_object_vars($this));$this->setFlags($ar[‘flag’]);
$this->exchangeArray($ar[‘storage’]);
$this->setIteratorClass($ar[‘iteratorClass’]);$need2continue=false;
foreach ($ar as $k => $v) {
$need2continue=false;
switch ($k) {
case ‘flag’:
$this->setFlags($v);
break;
case ‘storage’:
$this->exchangeArray($v);
break;
case ‘iteratorClass’:
$this->setIteratorClass($v);
break;
case ‘protectedProperties’:
/**
* in php 7.3 it shows
* Warning: “continue” targeting switch is equivalent to “break”. Did you mean to use “continue 2”? in /…location of zf2 ………../Zend/Stdlib/ArrayObject.php on line 426
*/
#continue;
$need2continue=true;
break;
default:
$this->__set($k, $v);
}
if(true==$need2continue) continue;
}
}
going to change ZF2.4.9 core
I decided to change ZF 2.4.9 core. I know its a wired decision. But I don’t have any choice, cause they didn’t provide any release for PHP 7x. But the change will noted in this site by adding post for future.
Today I’m changing –
class: Zend\Validator\EmailAddress
function: idnToAscii($email) and
function:Â idnToUtf8($email)
Previous (original) code –
protected function idnToAscii($email)
{
if (extension_loaded(‘intl’)) {
return (idn_to_ascii($email) ?: $email);
}
return $email;
}protected function idnToUtf8($email)
{
if (extension_loaded(‘intl’)) {
return idn_to_utf8($email);
}
return $email;
}
New Code
protected function idnToAscii($email)
{
if (extension_loaded(‘intl’)) {
return (idn_to_ascii($email, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46) ?: $email);
}
return $email;
}protected function idnToUtf8($email)
{
if (extension_loaded(‘intl’)) {
return idn_to_utf8($email, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
}
return $email;
}
I took this decision for –
https://bugs.php.net/bug.php?id=75609 and
https://wiki.php.net/rfc/deprecate-and-remove-intl_idna_variant_2003
Install PHP-Redis on PHP5.6 Centos 7
Assume that PHP 5.6 installed and working properly.
Now execute following command
sudo yum install php56w-devel sudo yum install git-core git clone git://github.com/nicolasff/phpredis.git cd phpredis/ sudo phpize sudo ./configure sudo make sudo make install sudo echo "extension=redis.so">/etc/php.d/redis.ini sudo apachectl restart
Its pretty simple!
PHP Archive (.phar) Attaching with ZF2
PHP archive aka phar is a stream wrapper which can serve any packaged PHP library efficiently.
To create a phar document for a library (not for web output or for cli executable) is simple as pie like –
$phar = new \Phar('target-location-where-to-save.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, 'optionalPharAliasName'); $phar->buildFromDirectory('source/lib/path');
After creating phar document, file can use to attach ZF2 standard autoloader as follows –
Zend\Loader\AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' => array( 'autoregister_zf' => true, 'namespaces' => array( 'YourProjectNamespace'=>'phar:///absolute/path/of/project/phar/file.phar', ), ), ));
This works as simple folder of your file system. Easily distributable, packaged.
PHP Debug efficiently with debug_backtrace
Hello today I write a small function for debug your PHP code. Its simple but powerful –
$bkTrace=function ($stack) { $output = ''; $stackLen = count($stack); for ($i = 1; $i < $stackLen; $i++) { $entry = $stack[$i]; $func = (array_key_exists('class', $entry)?$entry['class'].'\\':'').$entry['function'] . '('; $argsLen = count($entry['args']); for ($j = 0; $j < $argsLen; $j++) { $my_entry = $entry['args'][$j]; if (is_string($my_entry)) { $func .= $my_entry; } if ($j < $argsLen - 1) $func .= ', '; } $func .= ')'; $entry_file = 'NO_FILE'; if (array_key_exists('file', $entry)) { $entry_file = $entry['file']; } $entry_line = 'NO_LINE'; if (array_key_exists('line', $entry)) { $entry_line = $entry['line']; } $output .= $entry_file . ':' . $entry_line . ' - ' . $func . PHP_EOL; } return $output; }; echo '<pre>'.$bkTrace(debug_backtrace()); exit();
Also for response with warning you can use above method like below –
trigger_error(‘warning @’.$bkTrace(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)), E_USER_WARNING);
nJoy debugging…