Error: Trying to get property of non-object - php

I'm getting an error:
Trying to get property of non-object on line 30
On the commented line below...
function admin_trim_category_description( $terms, $taxonomies ){
if( 'category' != $taxonomies[0] ) return $terms;
$my_categories = array('item1','item2','item3');
foreach( $terms as $key => $term)
if(in_array(
$terms[$key]->name, //ERROR LINE HERE
$my_categories))
{
unset($terms[$key]);
}
}
What am I doing wrong?

$terms[$key] seems not to be an object. I'd suggest you to check whatever it is an object or not with:
if (is_object($terms[$key])) { /* OK */ }
and/or check whatever the object is an instance of a specific class with:
if ($terms[$key] instanceof MyClass) { /* OK */ }

Related

Fatal error: Call to undefined method Action::execute() /home/public_html/system/engine/event.php on line 62

Hi tried refreshing the modification cache cache in opencart and since then i am getting a blank page in front end with this error message.
public function trigger($event, array $args = array()) {
foreach ($this->data as $value) {
if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($value['trigger'], '/')) . '/', $event)) {
$result = $value['action']->execute($this->registry, $args);
if (!is_null($result) && !($result instanceof Exception)) {
return $result;
}
}
}
}
Thanks,
It seems your have an OC version 3.0.2.x or above.
In your $this->data of the Event Class, you have an event registered that is missing an action parameter.
$this->data[] = array(
'trigger' => $trigger,
'action' => $action, // <-- this must be an Action Object with a method execute()
'priority' => $priority
);
All events are registered via the register() method which explicitly requests that an Action object is being passed as a parameter.
Since the error is pointing to "Call to undefined method Action::execute()", I can assume, you have an issue with the action class.
Most likely you need to check the Modifications of the system/engine/action.php file in your system/storage/modifications.
It could be that the method execute() is either missing or somehow corrupt.
Debug
try to var_dump the $value to see what is there:
public function trigger($event, array $args = array()) {
foreach ($this->data as $value) {
//log out the $value before the error to see if the Action object is actually there and see what trigger causes this.
var_dump($value);
if (preg_match('/^' . str_replace(array('\*', '\?'), array('.*', '.'), preg_quote($value['trigger'], '/')) . '/', $event)) {
$result = $value['action']->execute($this->registry, $args);
if (!is_null($result) && !($result instanceof Exception)) {
return $result;
}
}
}
}
Hope this helps

Object could not be converted into string

code:
public function atualizarPorLocal( $acao, $novasRedes )
{
$em = $this->getEntityManager();
$apagaObj = $em->getRepository( 'CommonBundle:AcaoRede' )->findBy( array( 'acao'=>$acao ) );
foreach ( $apagaObj as $acao){
if (!empty($acao))
$em->remove($acao);
}
$em->flush();
foreach ( $novasRedes as $idRede )
{
$new = new AcaoRede();
$new->setAcao( $em->getRepository( 'CommonBundle:Acao' )->find( $acao ) );
$new->setRede( $em->getRepository( 'CommonBundle:Rede' )->find( $idRede ) );
$em->persist( $new );
}
$em->flush();
}
erro:
PHP Catchable fatal error: Object of class \CommonBundle\Entity\AcaoRede could not be converted to string in vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 2794, referer: /app_dev.php/modulo/editar/col_soft
what can I do to work around this error?
You have to use Acao's id
$new->setAcao( $em->getRepository( 'CommonBundle:Acao' )->find( $acao->getId() ) );
Doctrine's find method is expecting id field, so it tries to convert your $acao to string using __toString() method. You have to pass $acao->getId() to make it work
variable $acao was assuming a different value when entered in the second foreach
solved this problem by changing the name of the variable.
foreach ( $apagaObj as $acaoObj){
if (!empty($acaoObj))
$em->remove($acaoObj);
}

What's wrong with this PHP code on Line 136

I get the following error on my WP Dashboard:
Warning: First parameter must either be an object or the name of an existing class
in /home/content/88/11746388/html/wp-content/plugins/pluginname/pluginname.php on
line 136
Those are my lines from 130 to 140. Any ideas?
function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, 'response [$plugin_file]') && property_exists('response [$plugin_file]', 'new_version') ) {
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
$latest_version = $sr_plugin_info->response [$plugin_file]->new_version;
}
return $latest_version;
}
it seems that you have error in this line :
if ( property_exists($sr_plugin_info, 'response [$plugin_file]') ) {
property_exists — Checks if the object or class has a property .
and 'response [$plugin_file]' it is not a valid property name of class.
To be honest that code is bleeding from several wounds.
a.) get_site_transient is not guaranteed to return an object and property_exists expects one
b.) you try to check if an array key exists in a property with property_exists. I'm fairly certain that property_exists can not do that - you should only check for response property with it and use isset() for the $plugin_file index.
function get_latest_version($plugin_file) {
$latest_version = '';
$sr_plugin_info = get_site_transient ( 'update_plugins' );
// if ( property_exists($sr_plugin_info, response[$plugin_file]) && property_exists(response[$plugin_file], 'new_version') ) {
if ( property_exists($sr_plugin_info, response[$plugin_file]) ) {
$latest_version = $sr_plugin_info->response[$plugin_file]->new_version;
}
return $latest_version;
}

PHP error syntax error, unexpected T_FUNCTION in

/CustomPostType.php on line 165
// Initialise class variables as blank
$metaKeys = $this->get_meta_keys();
foreach( $metaKeys as $key )
if( !empty( $key ) )
$this->$key = null;
$this->ID = null;
The below code is what fixed the syntax error.
public function get_meta_keys( $objectName) {
$getClassVars = get_class_vars( $objectName );
return array_keys( $getClassVars);
}
There is syntax error in the code. It should be like this :
public function get_meta_keys( $objectName) {
$getClassVars = get_class_vars( $objectName );
return array_keys( $getClassVars);
}
Point to correct :
Correct function signature. Add argument in the function get_meta_keys().
Err, your code doesn't even look like it validates to me.
public function get_meta_keys( $objectName ) {
$getClassVars = get_class_vars( $objectName );
return array_keys( $getClassVars() );
}
Put semicolon at the end of the function call and also you need to format your code first.
public function get_meta_keys( )
{
$getClassVars = get_class_vars( $objectName );
return array_keys( $getClassVars() );
}

PHP: Is this "Undefined variable" exception a scope issue with my static method?

I have a static function which gets a few variables defined prior to going into a foreach() loop.
From within the foreach(), I try to call my $pnUuid var, and get
Unhandled Exception Undefined variable: pnUuid
I can successfully echo the var prior to the loop, as well as from within the loop if I immediately follow that print with die() or continue.
Calling global $pnUuid also circumvents the exception, but then the var is NULL :(
public static function find( $input )
{
// instantiate
$resultsByBrand = array();
$brandDictionary = new BrandHashTable();
// attempt to get a PN UUID
$partnumber = PartNumberEntity::where( 'number', '=', $input['partnumber'] )->first();
$pnUuid = empty($partnumber) ? false : $partnumber->uuid;
// get current inventory for the PN
$rzStocklines = RzStockEntity::where('fullpartnumber', '=', $input['partnumber'])
->get( array('fullpartnumber', 'manufacturer', 'quantity'));
// process the stocklines
foreach( $rzStocklines AS $row )
{
// do our darndest to get a UUID for the Brand
$brandDictionary->registerByName($row->manufacturer);
$brandUuid = $brandDictionary->getUuid($row->manufacturer);
// set a key of either the brand name, or the uuid
$key = $brandUuid ? $brandUuid : $row->manufacturer;
// instantiate the object for this brands results
if( !array_key_exists($key, $resultsByBrand))
{
$resultsByBrand[$key] = new stdClass();
$resultsByBrand[$key]->partnumber = $row->fullpartnumber;
$resultsByBrand[$key]->brand = $row->manufacturer;
$resultsByBrand[$key]->quantity = 0;
$resultsByBrand[$key]->minimum_order_quantity = 1;
}
// include the qty with the total sum
$resultsByBrand[$key]->quantity += $row->quantity;
// make sure we need Product-level info before doing the work to get it
if( !isset( $resultsByBrand[$key]->grams_piece_weight ))
{
//echo '<pre>Brand UUID:<br />',print_r($brandUuid, TRUE),'</pre>'; // For Testing ----->
//echo '<pre>PN UUID:<br />',print_r($pnUuid, TRUE),'</pre>'; // For Testing ----->
//die('(Dead by request)'); // For Testing ----->
/**
* This is a _**wierd**_ one ...
* An "Undefined Variable: $pnUuid" exception is getting thrown.
* If we call "die()" or "continue()", it goes away.
* Casting it as a global also works, but then it is NULL
*/
global $pnUuid; // hackity hack hack hack
// check for product-level info
if( $brandUuid && $pnUuid )
{
$product = ProductEntity::where(
function ($query) use ($brandUuid, $pnUuid) {
$query->where('partnumber_uuid', '=', $pnUuid);
$query->where('company_uuid_brand', '=', $brandUuid);
})
->first();
// add product-level info to $resultsByBrand[$key]
if( !empty( $product ))
$resultsByBrand[$key]->grams_piece_weight = $product->piece_weight;
} //if( $brandUuid && $pnUuid...
} //if( !property_exists...
unset( $brandUuid, $pnUuid );
} //foreach( $rzStocklines AS...
return $resultsByBrand;
}
What the heck is going on here ???
BTW, I also just tried it as a non-static method, and it still throws the same exception.
It's probably not defined in the second loop iteration because you're unsetting the variable in the loop...?!

Categories