I don't know why, but PHP triggers a Fatal Error because a Class Method doesn't exist.
But it clearly exists!
left.phtml:
<?php
$block = Block::getBlock('core/sidebar_modules');
foreach($block->getSidebar('left') AS $key => $value)
{
$_block = explode('_',$value->getName());
if(isset($_block[1]))
{
$_block[1] .= '_widget';
}
$loadBlock = Block::getBlock(implode('/',$_block)); // returns instance of Visio_Blog_Block_Recent_Widget
Debug::var_dump($loadBlock);
/*
returns:
object(Visio_Blog_Block_Recent_Widget)#33 (0) {
}
*/
echo $loadBlock->widgetContent();
/*
returns:
Fatal error: Call to a member function widgetContent() on a non-object in E:\docroot\vhosts\zend.local.host\htdocs\app\design\default\templates\left.phtml on line 13
*/
Debug::print_r(get_class_methods($loadBlock));
/*
returns:
Array
(
[0] => __construct
[1] => widgetContent
)
*/
}
?>
Widget.php (Visio_Blog_Block_Recent_Widget)
Class Visio_Blog_Block_Recent_Widget
{
public function __construct()
{
return $this;
}
public function widgetContent()
{
return 'content';
}
}
I have no clue why this happens?
Is it possible that this is an Error off my View Class while implementing nested view Templates.
I built the framework from scratch.
from the error it looks like that $loadBlock does not contain an instance of Visio_Blog_Block_Recent_Widget or any class, for that matter.
So the problem is around assigning.
Related
There's the following (example) class:
class klasse
{
private $var = 'doit';
function doit($param)
{
return md5($param);
}
function bla($param)
{
// HERES THE PROBLEM
return $this->{$this->var}($param);
}
}
// Create new instance
$klasse = new klasse;
// Start the "dynamical output"
echo $klasse->bla('test');
This works fine! But the problem is that I'd like to call the md5() function "directly dynamically". So I don't want to go the detour with "doit()".
If I try
private $var = 'md5';
at the beginning of the class I get the following (absolutely senseful) error message:
Fatal error: Call to undefined method klasse::md5() in - on line 13
So I know that this error is senseful but I have no clue how to avoid it?
How can I handle this (to directly call md5())?
Thank you!
This should work:
class klasse
{
public function __construct() {
$this->var = 'md5';
}
}
$klasse = new klasse;
echo call_user_func($klasse->var, 'argument');
More info at: http://php.net/manual/en/function.call-user-func.php
I got CMS and trying to install it, but when i try to login i got error
Cannot override final method Exception::getPrevious()
Fatal error: Cannot override final method Exception::getPrevious() in C:\wamp\www\uis-online\application\exceptions\applicationException.php on line 41
Does anyboy have idea what cause this error
code in this file is
class ApplicationException extends Exception
{
protected $innerException;
/**
* Konstruktor
* #return unknown_type
*/
public function __construct($message, $code = 0, Exception $innerException = null)
{
parent::__construct($message, $code);
if (!is_null($innerException))
{
$this->innerException = $innerException;
}
}
public function getPrevious()
{
return $this->innerException;
}
// custom string representation of object
public function __toString() {
$exceptions_message = "";
if($this->innerException != null && $this->innerException->getMessage() != "") {
$exceptions_message = $this->innerException->__toString();
}
return $exceptions_message .__CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
As shown in the documentation the method you're trying to override is a final one.
final public Exception Exception::getPrevious ( void )
http://php.net/manual/en/exception.getprevious.php
According to the PHP manual you can't override final methods.
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
http://php.net/manual/en/language.oop5.final.php
I have a problem in Yii framework, I want to call a controller's action in the layout/main.php page which is belong to the siteController, I did this:
$a = UsersController::actionRequestAlert($s);
then I got this error:
Non-static method UsersController::actionRequestAlert() should not be called statically, assuming $this from incompatible context
so how can I solve this problem?
ok,
now I want to create a widget, here is the steps I made:
created folder 'widgets' in folder 'protected'.
created folder 'views' in folder 'widgets'.
added this in config/main.php : 'application.widgets.*'
this is the code of widgets/Alert.php :
class AlertWidget extends CWidget
{
public $alert = null;
private $_data = null;
public function init()
{
$s = Yii::app()->session['userId'];
$r = Requests::model()->findAll('idUser='.$s.' and confirm =0 and unconfirm=0 and cancel=0');
$i=0;
foreach($r as $x)
$i++;
if($i<=0)
$alert=null;
else
$alert="(".$i.")";
$this->_data = new CActiveDataProvider($alert);
}
public function run()
{
$this->render('alert', ['data' => $this->_data]);
}
}
this is the code of widgets/views/alert.php:
echo $data;
this is the code to how I use the widget in a view:
$this->widget('application.widgets.Alert');
finally I got these errors:
( ! ) SCREAM: Error suppression ignored for
( ! ) Fatal error: Cannot redeclare class AlertWidget in C:\wamp\www\mediastore\protected\widgets\Alert.php on line 27
About first question:
You must define method actionRequestAlert() as static
public static actionRequestAlert() {}
I am getting this error: Access to undeclared static property: DBug::$errorMsg
Following is the code
class DBug
{
private static $errorMsg = array(
1 => 'inv-req',
2 => 'inv-reqPrm',
3 => 'no-set',
4 => 'less-h',
5 => 'less-w'
);
public static function showTinyErrMsg($errCode=0)
{
if(SHOW_ERROR_MSG_IN_RESPONSE === TRUE) {
if(array_key_exists($errCode, self::$errorMsg)) {
echo "// ".self::$errMsg[$errCode].";\n" ;
}
}
}
}
I call this function by DBug::showTinyErrMsg(1);. I get the above mentioned error. I am surely missing some OO rule, please help me with this.
P.s: The reason for this class having all static member is, that it's a long standing class with all static members, so I had to add this new method as static
The property is $errorMsg, but you're calling $errMsg.
The following code is a simplified example of what I'm trying to understand.
I'm using an external library that uses callbacks to process multiple requests. Ultimately I've been trying to figure out how to make Test->inc() call ExternalLib for each array element, then wait for all callbacks to be executed before continuing.
As you can see, the fatal error on line 18 is due to the method being called via call_user_func. How can I do this, or is there perhaps a better method?
class Test {
public $a = array();
public function inc(array $ints){
$request = new ExternalLib();
foreach ($ints as $int) {
$request->increment($int);
}
while( count($this->a) < count($ints) ) {
usleep(500000);
}
$test->dump();
}
public function incCallback($in, $out) {
/* append data to Test class array */
$this->a[] = array($in => out); /* Fatal error: Using $this when not in object context */
}
public function dump() {
/* Print to screen */
var_dump($this->a);
}
}
class ExternalLib {
/* This library's code should not be altered */
public function increment($num) {
call_user_func(array('Test','incCallback'), $num, $num++);
}
}
$test = new Test();
$test->inc(array(5,6,9));
Desired output:
array(3) {
[5]=>
int(6)
[6]=>
int(7)
[9]=>
int(10)
}
This code also available at codepad
The problem isn't a timing/waiting issue. It's a static vs. instantiated issue.
Calling the function using call_user_func(array('Test','incCallback')... is the same as calling Test::incCallback. You can't use $this when making a static call.
You will either need to modify the external lib to use an instantiated object or modify the Test class to use all static data.
Edit
I don't know exactly what you're looking to accomplish, but if making the class operate as a static class is your only option, then that's what you have to do ...
There are a couple other issues with your code:
Based on your desired output, you don't want a[] = array($in, $out) but rather a[$in] = $out
$num++ will not increment until after the function is called ... you want ++$num
Here is a working example ...
class Test {
public static $a;
public function inc(array $ints){
$request = new ExternalLib();
foreach ($ints as $int) {
$request->incriment($int);
}
while( count(self::$a) < count($ints) ) {}
self::dump();
}
public function incCallback($in, $out) {
/* append data to Test class array */
self::$a[$in] = $out;
}
public function dump() {
/* Print to screen */
var_dump(self::$a);
}
}
class ExternalLib {
/* This library's code should not be altered */
public function incriment($num) {
call_user_func(array('Test','incCallback'), $num, ++$num);
}
}
Test::$a = array();
Test::inc(array(5,6,9));