Call a member function on a non object - php

i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :
Fatal error: Call to a member function getIdentifiant() on a
non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34
Line 34 is here :
while($listActivities->next())
{
$current = new Activity();
$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}
And this is the getCurrent() function which return an Activity object.
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
}
I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.

echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.
Also :
$current = new Activity();
$current = $listActivities->getCurrent();
Doesn't really makes sense to me.
Use
var_dump($listActivities->getCurrent());
To see what it exactly returns, and trust what errors say.
EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.

first set $this->current after $current = new Activity(); (maybe in constercture)
and you should return false if !isset($this->activities[$this->current])
also if you use $current = $listActivities->getCurrent() you lost your Activity object , it should save into another variable
here new code :
while($listActivities->next())
{
$current = new Activity();
if( $listActivities->getCurrent() )
echo $current->getId(); // line 34
}
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
return false;
}

Related

Fatal Error On Zend Project Live On Server Random

I have a Zend2 project running on my localhost with no problems. The app runs perfect. I Uploaded it to my server and now it gets a fatal error but not every time.
Sometimes it says this,
Fatal error: Class name must be a valid object or a string in /home/public_html/vendor/zendframework/zend-stdlib/src/ArrayObject.php on line 230
public function getIterator()
{
$class = $this->iteratorClass;
return new $class($this->storage); // line 230
}
And sometimes it says this,
File
/vendor/zendframework/zend-stdlib/src/ArrayObject.php:184
Message:
Passed variable is not an array or object, using empty array instead
Never both and sometimes it loads perfectly with no problems. The file it references is in the vendor path this is the link,
public function exchangeArray($data)
{
if (!is_array($data) && !is_object($data)) {
throw new Exception\InvalidArgumentException('Passed variable is not an array or object, using empty array instead');
} // Line 184
if (is_object($data) && ($data instanceof self || $data instanceof \ArrayObject)) {
$data = $data->getArrayCopy();
}
if (!is_array($data)) {
$data = (array) $data;
}
$storage = $this->storage;
$this->storage = $data;
return $storage;
}
Any ideas why this would happen on a live server with a zend site but not on a localhost?
I found this post on github which I think it related to ZFCUser
Git Hub Post
Someone in the comments says this,
This issue is caused by the layout.phtml when there is an error. The layout needs to render but it doesn't have $this->url
I have no clue what he is talking about. Is anyone able to shoot me in the right direction?

How does one define a PECL bbcode extension callback when writing a CodeIgniter library?

I'm writing a CodeIgniter library around PHP's bbcode PECL extension, but I'm having some trouble with callbacks.
I set up the handler in the library constructor:
function __construct() {
$basic = array(
'url' => array(
'type' => BBCODE_TYPE_OPTARG,
'open_tag' => '<a href="{PARAM}" rel="nofollow">',
'close_tag' => '</a>',
'childs'=>'i,b,u,strike,center,img',
'param_handling' => array($this, 'url')
)
);
$this->handler = bbcode_create($basic);
}
public function parse($bbcode_string) {
return bbcode_parse($this->handler, htmlentities($bbcode_string));
}
As you notice, this uses a callback for handling what's allowed to go into the URL. I use this to insert an "exit redirect" page
public static function url($content, $argument) {
if (!$argument) $argument = $content;
$url = parse_url($argument);
if (!isset($url['host'])) {
if (strlen($argument) > 0 && $argument[0] != '/') return false;
$destination = '//'.$_SERVER['HTTP_HOST'].$argument;
} elseif ($url['host'] != $_SERVER['HTTP_HOST']) {
$destination = '//'.$_SERVER['HTTP_HOST'].'/exit?'.urlencode($argument);
} else {
$destination = $argument;
}
return htmlspecialchars($destination);
}
And I also have a little function which helps me test this out as I work:
function test() {
$string = '[url]http://www.google.com[/url]';
echo '<pre>';
die($this->parse($string));
}
This all works fine if the test() method is called from within the library. For example, if I throw $this->test() at the bottom of the constructor, everything works exactly as I would expect. However, calling $this->bbcode->test() from somewhere else (e.g. in a controller), I get the following errors:
**A PHP Error was encountered**
Severity: Warning
Message: Invalid callback , no array or string given
Filename: libraries/bbcode.php
Line Number: 122
**A PHP Error was encountered**
Severity: Warning
Message: bbcode_parse(): function `' is not callable
Filename: libraries/bbcode.php
Line Number: 122
http://www.google.com
The callback does not get executed, and as a result the link's href attribute is empty. Line 122 refers to the single line of code in my parse function:
return bbcode_parse($this->handler, htmlentities($bbcode_string));
How do I address this callback function such that it can be located when $this->bbcode->test() is called from inside a controller?
Now I'm even more confused...
So in the hopes of just putting this all behind me, I put these callback functions in a helper so I can just call them directly. So I now have code like this:
function __construct() {
$basic = array(
'url' => array(
'type' => BBCODE_TYPE_OPTARG,
'open_tag' => '<a href="{PARAM}" rel="nofollow">',
'close_tag' => '</a>',
'childs'=>'i,b,u,strike,center,img',
'param_handling' => 'bbcode_url'
)
);
$this->handler = bbcode_create($basic);
}
With the above, I get the following error:
**A PHP Error was encountered**
Severity: Warning
Message: Invalid callback 6.7949295043945E-5, no array or string given
Filename: libraries/bbcode.php
Line Number: 176
**A PHP Error was encountered**
Severity: Warning
Message: bbcode_parse(): function `6.7949295043945E-5' is not callable
Filename: libraries/bbcode.php
Line Number: 176
(line 176 is the new location of the parse() function)
Um... I don't even know what's going on. The number 6.7949295043945E-5 changes with every attempt.
The only solution I have found to this is quite simply not to set the handler up in a constructor. Instead, the parse() method contains both the bbcode_create() call and the bbcode_parse() call. That is, the bbcode_handler is freshly created for every string of bbcode to be parsed.
This seems needlessly wasteful to me. But, over the course of the lifetime of this project, it is exceedingly unlikely to cost even a tenth of the amount of time that I have spent trying to sort this out "properly", so I'm calling it a day.
I'm posting this "solution" here in case somebody else happens across this question and can thereby save themselves a few hours' pain. That said, I would really like to know what on earth is going on, and how to do this properly.

Fatal error: Call to a member function isUploaded() on a non-object [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
I'm facing an error message:
Fatal error: Call to a member function isUploaded() on a non-object in /www/htdocs/nether/http/123factuur/application/controllers/Helpers/ImportXls.php on line 30
To my understanind this error message pops-up because I'm calling a method which doesn't exists in the object. But I'm sure that isUploaded() does exists.
The function isUploaded is defined in the class Zend_Form_Element_File. To check if $xls is an instance of the Zend_Form_Element_File I debugged the $xls variable.
Zend_Debug::dump($xls); //OUTPUT: object(Zend_Form_Element_File)#141 (29) {
exit;
Line 30 looks like this:
if ( $xls->isUploaded() ) {
The first thing I did was to check the expression value.
Zend_Debug::dump($xls->isUploaded()); //the output was: bool(true)
exit;
Then I checked the type of the $xls variable.
echo gettype($xls); //the output was object
exit;
I'm not fully understanding the error. Perhaps, I'm not interpreting the error message as it should be interpreted. Anyway, assistance is needed.
The code snippet:
At the controller:
public function importAction() {
$form = $this->getImportFrom();
$this->view->form = $form;
$this->view->allowedHeaders = array();
$this->importInvoices($form);
$this->importInvoiceArticles($form);
$this->importInvoiceServices($form);
foreach ($this->_lookupIssues as $issue) {
$this->_flashMessenger->addMessage($issue);
}
}
public function importInvoiceArticles($form) {
$model = 'Invoice_article';
$config = Zim_Properties::getConfig($model);
$Model = new Zim_Model($model, $config->model);
$headerMapping = array_flip(array_intersect_key($Model->getHeaders(true), array_flip($this->_allowedArticleImportHeaders)));
$this->getHelper('ImportXls')->handleImport($form, $headerMapping, $Model->getName(), $this->_modelName, null, null, array($this, 'saveImportedArticleData'), 'invoiceArticle');
}
At the helper:
class F2g_Helper_ImportXls extends Zend_Controller_Action_Helper_Abstract {
public function handleImport($form, $allowedHeaders, $tableName, $modelName, $onDuplicateImportCallback, $importMethod = null, $saveMethod = null, $name = 'xls') {
if ($this->getRequest()->isPost()) {
$xls = $form->getElement($name);
if ( $xls->isUploaded() ) {
//some code
}
}
}
}
I'm quite sure that the handleImport() method is called multiple times, possibly inside a loop, probably with different values for the $name parameter. You echo the variable and die in order to debug it, which works perfectly if the provided value for $name is correct on the first run - but since you kill the script - you don't get any debug information about subsequent calls.
Make sure the object has that method before calling it. You can either call method_exists() or instanceof to make that determination.
Code:
if ($xls instanceof Zend_Form_Element_File) {
// object of correct type - continue (preferred version)
}
// or
if (method_exists($xls, 'isUploaded')) {
// avoids the error, but does not guarantee that
// other methods of the Zend_Form_Element_File exist
}
Add this to your condition to avoid the Fatal Error :
if ( !empty($xls) && is_object($xls) && $xls->isUploaded() ) {
// do your job with serenity :)
}

Magento Extension error: Call to a member function on a non-object

I have a Magento extension which is supposed to add a donation to PayPal order, but it's throwing an error in Model/Observer.php. It is when people are done with PayPal and are redirected back to my website. The URL when this error is shown is /paypal/express/placeOrder/. The error is Fatal error: Call to a member function getBaseDonation() on a non-object in [path]/Model/Observer.php on line 215. Line 215 is inside the if (!$donation) {
public function addPaypalItem($observer)
{
$cart = $observer->getEvent()->getPaypalCart();
$quote = $cart->getSalesEntity();
$donation = $quote->getBaseDonation();
if (!$donation) {
$donation = $quote->getShippingAddress()->getBaseDonation() ? $quote->getShippingAddress()->getBaseDonation() : $quote->getBillingAddress()->getBaseDonation();
}
if ($donation > 0) {
$cart->addItem(
Mage::helper('donations')->__('Donation'),
1,
$donation
);
}
return $this;
}
How can I solve this non-object problem? Thank you!
First I think you should recheck the getShippingAddress() function to ensure it returns an object in our working flow.
If it is, let check if your extension already override to ShippingAddress object to input the BaseDonation value.
$quote->getBaseDonation() is different from $quote->getShippingAddress()->getBaseDonation();

PHP Get corresponding data, with default and error handling

I have a normal HTML menu that passes a GET statement to the url.
<li>Home</li>
Just like this, al though this is ofcourse only 1 item of the entire menu.
In a seperated file I have an function that checks if an GET or POST statement exist,
and If it exist and is not NULL then it will give the value back to where it was called.
public function getFormVariable($value){
switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'GET':
if (isset($_GET[$value]) && $_GET[$value] != NULL) {
return $_GET[$value];
}
else{
return false;
}
break;
case 'POST':
if (isset($POST[$value]) && $POST[$value] != NULL) {
return $POST[$value];
}
else{
return false;
}
break;
default:
return false;
}
}
And with the following code it takes the get value and finds the corrosponding class
(every class is in a seperated file, and every class is 1 link in my menu)
In this class there is just some regular functions/data that gives the content of that page.
$class = loadClass($ConfigPage->getFormVariable('Page'));
$ConfigPage->SetProperty('content', $class);
function loadClass($Page){
$class_name = 'Content' . $Page;
if(!class_exists($class_name)){
return 'Error: Content has not been found.';
}
$class = new $class_name();
return $class;
}
Explaining: The menu gives a GET value of 'Contact' which is then check by GetFormVariable() and then the corresponding class is found which gives back the content that class holds.
Now my question:
When the function LoadClass() cant find the name of the class it was given through the GET statement, it should return a error string. But this is not happening. I get a beautiful big orange error from PHP saying the following:
Fatal error: Call to a member function render() on a non-object in
E:\Program files\wamp\www\BDW\Class\Html_Page.Class.php on line 147
Line 147 is where to object is called
echo $this->content->render();
The Render function is as it says a normal return function inside the content classes.
Why do i get this error, and how do i fix it?
Second question. If there is no GET statement in the url. It gives the exact same error. which is pretty logical. But how do i make it show ContentHome when there is no GET statement in the url, and an ERROR when the value of the GET statement is incorrect.
Thank you for reading,
If there is anything unclear please tell me. English is not my native language, and after all. I am here to learn.
EDIT:
My knowledge of PHP is not great enough, so i decided when a class can not be found. it brings you back to home without any error. which i wanted in the first place.
Here is my new code which is still not working, why?
$class = loadClass($ConfigPage->getFormVariable('Page'));
$ConfigPage->SetProperty('content', $class);
function loadClass($Page){
$class_name = 'Content' . $Page;
$Default_Class = 'ContentHome';
if(!class_exists($class_name)){
//echo 'Error: Content has not been found.';
$class = new $Default_Class();
return $class;
}
else{
$class = new $class_name();
return $class;
}
$ConfigPage->Render();
}
It's happening because of this line :
if(!class_exists($class_name)){
return 'Error: Content has not been found.';
}
In the case the class doesn't exist, you're returning a string from the function, and afterwards trying to call a method render() on it. You can fix it either by changing this behaviour (don't return an error string, but use an Exception or trigger_error() ) or checking that the function loadClass() does return a valid object through is_object() for example.
Regarding your second question, you should expand your tests in loadClass() to handle the empty GET variable case, and substitute the empty string with a "Home" default value.
Update :
Example usage for is_object in your case :
$class = loadClass($ConfigPage->getFormVariable('Page'));
if(! is_object($class)) {
// if $class is not an object, then something went wrong above
throw new \Exception('Invalid data returned from loadClass()');
}
$ConfigPage->SetProperty('content', $class);

Categories