Invalid Memcache->connection member variable errors - php

We are currently using Nginx as our server and memcached for caching mechanism. I was inspecting PHP error logs and lots of PHP Warnings about memcached caught my attention.
PHP Warning: Memcache::get(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 180
At the line it was pointing, there is this piece of code:
$tmp = $this->_memcache->get($id);
I also see many other PHP warnings with the same warning message but different with different function calls of memcache object:
PHP Warning: Memcache::add(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 180
PHP Warning: Memcache::set(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 230
PHP Warning: Memcache::delete(): Invalid Memcache->connection member variable in {directory}/library/Zend/Cache/Backend/Memcached.php on line 323
I did a search through the web but could not find anything that really helped. From time to time, we have some problems with our memcached. Is it possible that this some kind of issue that happens when servers are down because of some problem? I really do not have any idea about what causes these warnings. How can I correct it or at least how can I avoid these warnings?

I found 3 references that might help you
Limit of file system and memcache
Possible memory leaks
Memcache vs Memcached

You need to check key max 250 chars and value max : 1MB

Have you compiled your own php recently? It's possible ther versions are out of sync.

I had the same problem.
when i called memcache object in __destruct to update the state of my object i goot the error.
and here is my solution.:
call object in your class function where you change the state and be sure to send an instance of memcache to this class.

While from the warning message itself, it may not be obvious, but the error may be happening when you're trying to serialize/deserialize memcache connection object itself.
For example:
class a {
private Memcache $mc;
private $name = 'glen';
public function __construct(Memcache $mc) {
$this->mc = $mc;
}
}
$a = new a($mc);
$mc->set('a', $a);
You very likely (as me) ended up here because class having mixed concerns (the object being a model and has as other business logic as well). you can omit the unwanted mc key from serializing using __debugInfo function:
public function __debugInfo() {
return [];
}
https://www.php.net/manual/en/language.oop5.magic.php#object.debuginfo
While writing this note, I'm not able to reproduce with my own example, so there's something else involved, perhaps memory corruption. But removing $mc property solve the problem for me.

Related

Accessing the $_SESSION variable from pthreads

I'm having an issue accessing the session variable from functions called from Thread objects using the pthreads library for PHP.
When the function is called from the main thread, no errors occur and everything runs fine.
When run from a Thread object however, I get the following errors:
> PHP Notice: Undefined variable: _SESSION
> PHP Notice: Undefined index: Properties Manager
> PHP Fatal error: Call to a member function getGroupValue() on a non-object
The line numbers specified by the errors all point to this code block:
function connect_mysql_db($database, $write = false) {
$properties = $_SESSION['Properties Manager'];
if(!isset($database) || strlen($database)==0){
throw new Exception("No database specified");
}
// Read appropriate host, port, dbname, user & pass for this database
$host = $properties->getGroupValue($database, DB_HOST);
$port = $properties->getGroupValue($database, DB_PORT);
$db_name = $properties->getGroupValue($database, DB_NAME);
...Removed unnecessary code...
}
A little searching says that I should be able to remedy this issue by putting session_start(); at the top of my file.
After doing this, the other errors are still printed in addition to:
> PHP Notice: A session had already been started - ignoring session_start()
So my main question: Is there something special that I need to do when using pthreads in order to access the super-global session? Or is there something completely different at play here that I am missing?
Edit:
Yes, I have tried global $_SESSION; as well.
After a bit more research, it seems that the session variable is not thread safe and in fact locks entirely until the session is closed. I had to remove references to the session and simply pass the needed information to the function or reinitialize it.
Luckily there isn't a performance downfall of the property manager being created.

Laravel 3 - PHP Warning: in_array() expects parameter 2 to be array

I am working with a code that was not written by me. It is a Laravel 3 application, it is giving me the following error:
PHP Warning: in_array() expects parameter 2 to be array, null given in /Users/use/Documents/project/laravel/error.php on line 87
Can you give me pointers on how to debug it? It is giving error in the file which was included in the framework. Also I using the php's inbuilt server to run the application. Does that cause problems? Any pointers are helpful.
PS: I am on a mac.
After looking at the source code it appears that you are getting the error from this file:
// laravel/error.php
// line 86
if (in_array($code, Config::get('error.ignore')))
{
return static::log($exception);
}
The method appears to be looking for the error.ignore config variable:
// application/config/error.php
// line 16
'ignore' => array(),
Check the application/config/error.php file, make sure that 'ignore' is set to an array. If it already is, then you likely have an error in one of the other config files which is corrupting the array.
Hopefully this helps and shows the steps you can take in tracking down the source of an error.
You can get this also if you're using composer and the required files have not been installed after a fresh checkout of the project. To resolve:
$ composer install
Or similar. To actually get a head on this information I had to throw $exception in the error.php file around line 87, then I saw the reason in the Apache error logs.
Most of this errors is about of parameters definition bug.
for example sometimes coder write
protected $guarded = 'title';
instead of
protected $guarded = ['title'];
That makes the paroblem

PHP-Yii: Using Yiis logger when a request is not completed due to php level error

This is probably a yii specific question, even though I wouldn't surprised if experienced non-Yii we developers will be also able to contribute.
I often encounter the following situation:
My application fails due to a fatal error on the php level. Something like $var->property when $var==null
I want to understand how $var came to be ==null.
I'd use logs for this, however, problem is that no logs are left when a request is ended due to php error.
edit: this only happens for fatal errors. For other php errors I have my logs back
An example:
For
$nonExistingVar->someProperty;
I do have my logs recorded, as it yields PHP Error Undefined variable: nonExistingVar
However, if I do define the variable and set it to null,
$tmp = null;
$tmp->prop;
Then I loose the logs, as it results in "Fatal error: Call to a member function hasErrors() on a non-object"
Does anybody understand why does it happen? And how can the logger be anyway used in this situations? I tried setting autoFlush=1, doesn't help
Thanks
Gidi
The below allowed me to have my logs even on fatal errors:
function yiiCorrectShutdown()
{
Yii::app()->end();
//the following line will work as well
//Yii::app()->log->processLogs(null);
}
register_shutdown_function('yiiCorrectShutdown');
I wrote an extension class that logs in real-time thus avoiding any need to modify flow paths. I posted it here on the Yii Wiki

Fatal error with Custom Magento Module on one server but not the other

I am creating my own custom module in Magento and during testing on a Litespeed server (PHP v5.2.14) I am getting a Fatal Error: Call to a member function batch() on a non-object in ../../../BatchController.php on line 25 that was not appearing during testing on another linux server and a wamp server (PHP v5.2.11).
This one has stumped me. I am guessing it has something to do with the server configuration rather than the code itself. But i am just guessing. I was hoping someone here could tell me.
The only real major difference I could see, aside from the php versions and environment, is that the server that the error is on is using the Suhosin Patch. But would that be something that could cause this?
The line in question is Mage::getModel('mymodule/mymodel')->batch(); which is enclosed in an IF statement. batch() is a public function located in my model file.
If you need more code let me know.
Thanks!
If you get a "non-object" error when calling a model, there's a problem with Magento's attempt to get your model class, and it is returning null. The reasons for this are not always apparent. If this worked identically on a normal LAMP stack, then the problem is most likely not in your code.
My first guess would be that the file does not have the proper permissions. Otherwise, it may have to do with resolving the classname. You could test this temporarily by calling the plugin directly like this:
$obj = new Mynamespace_Mymodule_Model_Mymodel();
$obj->batch();
If this works, then the file is readable, and you will want to go spelunking in the resolution of that classname. If it doesn't work, you have a problem with either autoloading or the declaration of your class.
Hope that helps!
Thanks,
Joe
Break it down.
You've tried to call
Mage::getModel('mymodule/mymodel')->batch();
and PHP told you it tried to call the method batch on a non-object. That means
Mage::getModel('mymodule/mymodel')
isn't returning a Model object the way it's supposed to.
First thing to do is clear out your Magento cache on the server you're having problems with. If your Module's config hasn't been loaded into the global config tree Magento will try to instantiate a Mage_Core_Model_Mymodel, and fail.
Second step is to make sure your module's app/etc/module file is in place.
Third step is to add some debugging (assuming a 1.4 branch) to the method that instantiates your objects and determine why Magento can't create your object
File: app/code/core/Mage/Core/Model/Config.php
...
public function getModelInstance($modelClass='', $constructArguments=array())
{
$className = $this->getModelClassName($modelClass);
if (class_exists($className)) {
Varien_Profiler::start('CORE::create_object_of::'.$className);
$obj = new $className($constructArguments);
Varien_Profiler::stop('CORE::create_object_of::'.$className);
return $obj;
} else {
#throw Mage::exception('Mage_Core', Mage::helper('core')->__('Model class does not exist: %s.', $modelClass));
return false;
}
}
...

Can I get PHP to throw an error when I try to define a function that has already been defined?

I run into this problem periodically, and I'm trying to figure out if it's a configuration issue or a peculiarity with PHP.
Basically, there will be a function foo() defined somewhere in my code, and then elsewhere I will accidentally define a function again with the same name foo(). I would expect an error of some kind to be thrown, e.g. "Fatal Error: Function already defined line 234, bar.php blah blah".
But I get nothing. I just get a blank screen. Debugging can take an eternity as I try to pinpoint exactly where the function is being accidentally redefined, without help from an error message.
My config settings for reporting errors are set to E_ALL, and I see all other kinds of errors without a hitch.
Any insights? Is there anything I can do to cause PHP to report these errors (so that I can either rename the offending function or wrap it in an if function_exists() clause)?
Edit: To be clear, I understand the many strategies to avoid namespace collisions in PHP. I'm talking about instances where for whatever reason I accidentally name a function a name that already exists during development (or for example I import a file where a function has already been defined). I would expect an error message when this occurs, which would help me debug, but I do not get one.
You can wrap function definitions in a conditional function_exists():
if (!function_exists("file_get_contents"))
{
function file_get_contents(....)
....
}
works for me.
You could wrap a function_exists check around each function and have it throw an error message if it already exists. The better way however would be finding out why fatal errors don't appear on screen. Can you check phpinfo() for the error_reporting and related settings?
This is what you should be getting when trying to redefine a function:
Fatal error: Cannot redeclare file_get_contents() in D:\xyz\htdocs\test.php on line 5
From my phpinfo():
display_errors On
error_reporting 22519 (equals... well, I don't know but it shows errors :)
In some hosting environments when error reporting was turned off completely, I have found defining a custom error handler very helpful: set_error_handler()
Are you hosting the script on your local server? I know a UK based hosting company that prevent any PHP errors from being returned at all, even if you've set E_ALL.
If this is the case consider testing the app locally with error reporting turned on.
As an alternative, you can actually check whether a function is already there by using function_exists.
if (function_exists('foo'))
{
echo 'foo function exists !!';
}
else
{
echo 'foo function does not exists !!';
}
Some suggestions to make your life easier:
I typically will create a generic functions file, where I store all my global functions that I'll be using throughout my app.
I'll also try and remain as object-oriented as possible. PHP will give errors if you're creating an object that already exists, and by encapsulating your functions into logical objects you'll have an easier time maintaining it.
It seems you're not organizing your code properly, try using a common library that you include in your files, and define functions there. Perhaps you just need to start uses classes, then you won't have collisions when you have two functions with the same name.

Categories