I have a legacy site (not written by me) that has been on a server with php5 on it for the last several years. I am in the process of creating a new server with php7 on it and testing what works and is broken.
the site uses pear by including the file pear/lib/DB.php. i created a brand new page that only has the code
<?php
require_once( "DB.php" );
?>
this presents the exact same error as the full site.
the error that's being presented is
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /local/sites/php/pear/lib/DB.php on line 310
the site only requires DB.php because I have added Pear to the php.ini in include_path
checking the version of Pear gives me the following
$ pear version
PEAR Version: 1.10.3
PHP Version: 7.0.15-0ubuntu0.16.04.4
Zend Engine Version: 3.0.0
Running on: Linux cdc-migration-0d 3.13.0-103-generic #150-Ubuntu SMP Thu Nov 24 10:34:17 UTC 2016 x86_64
from my research it shows the latest version of Pear is php7 compatible, so these should work together. any idea why just requiring the DB.php on a test page would immediately generate the parsing error?
edit:
the code in the pear file that is generating the error is as follows
function &factory($type, $options = false)
{
if (!is_array($options)) {
$options = array('persistent' => $options);
}
if (isset($options['debug']) && $options['debug'] >= 2) {
// expose php errors with sufficient debug level
include_once "DB/{$type}.php";
} else {
#include_once "DB/{$type}.php";
}
$classname = "DB_${type}";
if (!class_exists($classname)) {
$tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
"Unable to include the DB/{$type}.php file",
'DB_Error', true);
return $tmp;
}
#$obj =& new $classname; // ##### this is line 310 that generates the error #####
foreach ($options as $option => $value) {
$test = $obj->setOption($option, $value);
if (DB::isError($test)) {
return $test;
}
}
return $obj;
}
#$obj =& new $classname;
Assigning the return value of new by reference is deprecated since PHP 5.3. http://php.net/manual/en/migration53.deprecated.php
This is PHP4 style of writing PHP.
Write instead :
$obj = new $classname;
This has been removed as of PHP7.
See: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref
Related
I have installed fresh xampp (7.0.2 atm). I've created php-cli.ini, added pthread extension there and set memory limit to 3 gb. But when I'am trying to launch thread script I got this:
PHP Fatal error: Uncaught RuntimeException: cannot start my_thread, out of reso
urces in C:\xampp\htdocs\w\start_threads.php:160
Stack trace:
#0 C:\xampp\htdocs\w\start_threads.php(160): Thread->start()
#1 {main}
thrown in C:\xampp\htdocs\w\start_threads.php on line 160
Fatal error: Uncaught RuntimeException: cannot start my_thread, out of resources
in C:\xampp\htdocs\w\start_threads.php:160
(I'am using pthreds 3.1.5 x86)
What am I doing wrong here? Thank you!
Essentially, this is caused by pthread_create returning EAGAIN: It means that the system lacks resources to create another thread, or that the system imposed limit on the maximum number of threads (in a process, or system wide) has been reached.
This can be caused by two things, the purposeful use of more threads than a process can handle simultaneously as a result of the way some software is designed, or more perniciously, as a result of less than graceful joining of threads.
If you only seem to hit such errors sometimes, it would suggest the latter is going on; Be sure to cleanup (explicitly join) threads you are done with to make behaviour predictable.
My PHP version: 7.2.6 x82
And pthreads: php_pthreads-3.1.6-7.2-ts-vc15-x86
I created 25 threads, when created 21th thread then occurred same error.
I thought that it only can create 20 threads.
So I edited my code and that error does not occur
My code:
class ReadAllFile extends Thread {
public $folder;
public function __construct($folder) {
$this->folder = $folder;
}
public function run() {
//code process
}
}
$dir = "F:/sbd/sbdstore/20180606/";
$subFolders = scandir ( $dir );
$stack = array();
foreach ( $subFolders as $folder ) {
if ($folder != '.' && $folder != '..') {
$stack[] = new ReadAllFile ( $dir.$folder );
}
}
$maxNumberOfThread = 20;
$numberOfRunning = 0;
$numberOfStack = count($stack);
$elementIsStarted = array();
$allElementIsProcess = false;
while(count($stack)){
if($numberOfRunning <= $maxNumberOfThread && !$allElementIsProcess){
for($i=0;$i<$numberOfStack;$i++){
if(!in_array($i,$elementIsStarted)){
$numberOfRunning++;
$elementIsStarted[] = $i;
$stack[$i]->start();
if($i == $numberOfStack - 1){
$allElementIsProcess = true;
}
$i = $numberOfStack + 1;
}
}
}else{
foreach($elementIsStarted AS $element){
if(isset($stack[$element]) && $stack[$element]->isRunning() !== true){
unset($stack[$element]);
$numberOfRunning--;
}
}
}
}
Hope this help.
Sorry about my English.
P/s: If I use PHP version: 7.2.6 x64 and php_pthreads-3.1.6-7.2-ts-vc15-x64 then it does not occur this error.
I think x64 allocation more memory.
I am trying to write a simple application with wxPHP that pings IPs from a file using exec('ping') command.
I want the results to show up in the GUI one by one as they get pinged in the background so I'm doing the exec('ping') command in a thread.
But I get very strange errors and behavior. The problem is I can't reproduce the errors. Sometimes the program works totally fine. Sometimes it crashes in the thread. Sometimes it crashes when it is sending the event. etc.
Here are the errors I get when I run the program from command line:
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 0
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 0
C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice: Undefined property: wxHtmlWindow::$parent in Unknown on line 553649674
PHP Fatal error: Call to a member function setPingResults() on null in Unknown
on line 553649674
And this is my code:
<?php
if(!extension_loaded('wxwidgets'))
{
dl('wxwidgets.' . PHP_SHLIB_SUFFIX);
}
define('EVT_PINGDONE',wxNewEventType());
class myPing extends wxThread
{
function __construct($parent)
{
parent::__construct(wxTHREAD_JOINABLE);
$this->parent = $parent;
}
function Entry()
{
$addresses = file($this->parent->btnBrowse->GetPath(),FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach($addresses as $k => $address){
$results = exec("ping $address");
$this->parent->setPingResults($results);
$evt = new wxCommandEvent(EVT_PINGDONE);
$this->parent->QueueEvent($evt);
}
$this->parent->onThreadDone();
return;
}
}
class mythFrame extends wxFrame {
function __construct( $parent=null ){
parent::__construct ( $parent, wxID_ANY, 'Pinger', wxDefaultPosition, new wxSize( 600,400 ), wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
$this->SetSizeHints( wxDefaultSize, wxDefaultSize );
$bSizer2 = new wxBoxSizer( wxHORIZONTAL );
$this->button = new wxButton( $this, wxID_ANY, "Ping", wxDefaultPosition, new wxSize(300, 40), 0 );
$bSizer2->Add($this->button, 0, wxALL|wxEXPAND, 5);
$bSizer1 = new wxBoxSizer( wxVERTICAL );
$this->btnBrowse = new wxFilePickerCtrl( $this, wxID_ANY, 'C:\Users\SH\Desktop\stuff\servers.txt', "Select a file", "*.*", wxDefaultPosition, wxDefaultSize, wxFLP_DEFAULT_STYLE );
$bSizer1->Add( $this->btnBrowse, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL, 5 );
$this->html = new wxHtmlWindow( $this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->html, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5 );
$bSizer1->Add( $bSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );
$this->SetSizer( $bSizer1 );
$this->Layout();
$this->Centre( wxBOTH );
// Connect Events
$this->button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "hndlrButton") );
$this->Connect(wxEVT_TIMER, array($this, "onTimer"));
$this->Connect(EVT_PINGDONE, array($this, 'updateText'));
$this->pingThread = new myPing($this);
$this->m_timer = new wxTimer($this);
$this->pingResults = '';
}
function updateText($event){
$this->html->AppendToPage($this->pingResults.'<br>');
}
function onTimer(){
if($this->threadDone == true){
$this->m_timer->Stop();
$this->button->Enable();
$this->btnBrowse->Enable();
$this->html->AppendToPage('<br>Finished.<hr>');
while($this->pingThread->IsRunning()){}
$this->pingThread->Delete();
$this->pingThread = new myPing($this);
}
}
function onThreadDone(){
$this->threadDone = true;
}
function setPingResults($results){
$this->pingResults = $results;
}
function hndlrButton( $event ){
$this->threadDone = false;
$this->m_timer->start(3000);
$this->button->Disable();
$this->btnBrowse->Disable();
$this->pingThread->Create();
$this->pingThread->Run();
}
}
$myFrame = new mythFrame();
$myFrame->show();
wxEntry();
?>
And this is the errors I get in windows error log:
Faulting application name: wxphp.exe, version: 5.6.9.0, time stamp: 0x55765568
Faulting module name: ntdll.dll, version: 6.1.7601.18247, time stamp: 0x521eaf24
Exception code: 0xc0000005
Fault offset: 0x0000000000052f86
Faulting process id: 0x3c4
Faulting application start time: 0x01d0b6675e88094f
Faulting application path: C:\Program Files\wxPHP\php\wxphp.exe
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: 9cd6fe5e-225a-11e5-b316-f46d04f70903
Faulting module changes depending on whether i call php functions or call exec() in the thread Entry(). But the exception code is always 0xc0000005.
One weird thing that is consistent in errors is errors like
Undefined property wxHtmlWindow::$parent
If you read the code you realize that there is no such call at all. Actually $parent is only accessed in the thread. I have got similar errors with other functions in the thread. It seems like the application doesn't realize it is in the thread and $this in the thread points to the main frame object.
I guess something is terribly wrong with my code or maybe with wxPHP. This program was not working last night, then started working this morning. Then I added an icon and some background colors to it and it suddenly is giving me errors again.
Other info
Doesn't work on:
Windows 7-64bit - wxphp-3.0.2.0-php5.6-x64.exe
Windows 7-32bit - wxphp-3.0.2.0-php5.4-x86
The example thread.wxphp application bundled with the wxPHP
package doesn't work either.
However both my code and the example app work fine on Linux (Linux
Mint-32bit - php5-wxwidgets_3.0.2.0_i386).
When I remove both exec() and QueueEvent() from the thread
Entry() I don't get any errors.
Does anyone have any idea what's going on here?
I'm the maintainer of wxPHP and I would advice to use the PHP pthreads extension for multithreading given that this extension properly handles threading with the PHP zendengine. The wxThread class was automatically wrapped by the wxPHP source generator and it may need further development to properly work.
I should change sphinxapi.php , my sphinxapi.php on (usr/local/sphinx/lib/sphinxapi.php) i changed it but what should i do after that to use new functions ?
my php :
<?php
$sphinx = new SphinxClient();
$sphinx->SetServer($this->config->sphinx->host, $this->config->sphinx->port);
$sphinx->SetMatchMode(SPH_MATCH_ALL);
$sphinx->SetLimits(0, 1,1);
..filters...
$sphinx->RemoveFilter($color['id']);
My new function :
function RemoveFilter ( $attribute )
{
assert ( is_string($attribute) );
foreach($this->_filters AS $key => $filter){
if($filter['attr'] == $attribute){
unset($this->_filters[$key]);
break;
}
}
}
Error :
Fatal error: Call to undefined method SphinxClient::RemoveFilter() in
At a guess, you've modified the one that comes with sphinx, but the application itself is using a different 'sphinxapi.php' - maybe a locally installed one.
Or even you have the sphinx extension installed, so its providing the SphinxClient not 'sphinxapi.php`'- if so uninstall the extension.
I'm trying to write some code to track dependencies. Is there a way to programatically detect if a PEAR package has been installed? I'm thinking something like:
if ($some_pear_api->isPackageInstalled('FooPack')) {
echo 'FooPack is installed!';
} else {
echo 'FooPack is not installed. :(';
}
I know you can simply detect if the class file for that package exists, but I mostly want to know if PEAR has that installed because sometimes some libraries provide other means of including their code (e.g. PHPUnit has a pear channel as well as a git repo.).
Thanks for the help!
You need to use the PEAR_Registry class to do this (which is what the PEAR script itself uses).
Read Adam Harvey's blog post "pear -> list" from 3 years ago - all the details/examples you need are there.
include 'PEAR/Registry.php';
$reg = new PEAR_Registry;
foreach ($reg->listPackages() as $package) {
print "$package\n";
}
If you need this to check for specific versions of each package, then you could base something on the following example, which I provided in a comment to that blog entry:
<?php
require 'PEAR/Registry.php';
$reg = new PEAR_Registry;
define("NAME", 0);
define("VERSION", 1);
$packages = array(
array("PEAR", "1.6.2"),
array("Date", "1.4.7"),
array("Date_Holidays", "0.17.1"),
array("Validate_IE", "0.3.1")
);
foreach ($packages as $package) {
$pkg = $reg->getPackage($package[NAME]);
$version = $pkg->getVersion();
echo "{$package[NAME]} – {$package[VERSION]} – ";
echo version_compare($version, $package[VERSION], '>=') ? 'OK': 'BAD', "\n";
}
?>
If you need to copy and paste this, then it might be best for you to use the version at https://gist.github.com/kenguest/1671361.
You can use Pear/Infos packageInstalled to answer this:
<?php
require_once 'PEAR/Info.php';
$res = PEAR_Info::packageInstalled('FooPack');
if ($res) {
print "Package FooPack is installed \n";
} else {
print "Package FooPack is not yet installed \n";
}
?>
Why not just include the package and see if the class exists?
// Supress Errors. Checking is done below.
#require_once 'PHP/UML.php';
if(!class_exists('PHP_UML'))
{
throw new Exception('PHP_UML is not installed. Please call `pear install PHP_UML` from the command line',1);
}
// Code to use PHP_UML below...
$uml = new PHP_UML();
Need some help with this error. Fresh wordpress 2.9 install...
Fatal error: Cannot instantiate non-existent class: directoryiterator in /home1/test/public_html/test2/wp-content/themes/mytheme/functions.php on line 471
function get_dirs($path = '.') {
$dirs = array();
foreach (new DirectoryIterator($path) as $file) { //THIS IS LINE 471
if ($file->isDir() && !$file->isDot()) {
$dirs[] = $file->getFilename();
}
}
return $dirs;
}
The DirectoryIterator class is part of the Standard PHP Library (SPL), which is installed by default in PHP 5. Maybe you are using PHP 4?
EDIT
To get information on your PHP installation, use phpinfo(). Create a .php file with this single line and open it in your browser:
<?php phpinfo() ?>