I have the following code but it does not seem to want to work chained.
$this->view->setData($class_vars);
$this->view->render('addview');
The above works and runs fine but when i try to do the following:
$this->view->setData($class_vars)->render('addview');
I get the following error:
Fatal error: Call to a member function render() on a non-object in....
But the strange thing is when i call it the other way:
$this->view->render('addview')->setData($class_vars);
It runs, but I need the setData to run first as this sets up the var for the actual view, so even though i get the view its got errors where the vars should be? Both methods are public?
Thanks You
Does setData() return the view object (i.e. it has return $this; line)? If not... well it should if you want it to work this way.
For further reference. This technique is called 'fluent interface' and is described here:
http://www.martinfowler.com/bliki/FluentInterface.html
Related
I can't figure this out,
I have a method on my class like:
public function __call($closure, $args){
return call_user_func_array($this->{$closure}->bindTo($this),$args);
}
This works on my local server on magento, but when I try to use it on my server
it returns me the following error:
Fatal error: Call to undefined method Closure::bindTo() in
I tried adding var_dump to the variables, all have the correct values.
Any ideas?
Closure::bindTo exists in PHP 5.4 or newer only. Check your PHP version.
http://php.net/manual/en/closure.bindto.php
In my Laravel 4 project I've bound the current user to the views using the share() method like so:
View::share(['currentUser' => Sentry::getUser()]);
This works when browsing the site, all the views have access to the variable $currentUser. However, when attempting to test my application, the variable is never bound, despite a user definitely being logged in.
class PagesControllerTest extends TestCase
{
public function setUp()
{
parent::setUp();
// This works, as halting the application and dumping the user manually demonstrate it as such.
Sentry::login(User::first());
}
public function testIndex()
{
$this->get('/');
$this->assertResponseOk();
}
}
However, this simply results in a stack-trace of errors...
ErrorException: Trying to get property of non-object (View: ~/Sites/laravel/app/views/layouts/application.blade.php) (View: ~/Sites/laravel/app/views/layouts/application.blade.php)
...
Caused by
ErrorException: Trying to get property of non-object (View: ~/Sites/laravel/app/views/layouts/application.blade.php)
...
Caused by
ErrorException: Trying to get property of non-object
The exact line this fails at is where the view tries to access the $currentUser variable.
If I use a view composer instead, like follows, it solves the problem in this instance - but I want the variable available in ALL views, not just the ones I specify, and I'd also like to know WHY this is occurring.
View::composer('layouts.application', function($view)
{
$view->with('currentUser', Sentry::currentUser());
});
I'm guessing you are doing View::share in app/start/global.php. This file is invoked by calling parent::setUp(), which is before you've done Sentry::login, and thus, $currentUser will be null. You should either find a way to delay the View::share (using a view composer is one way to do this) or just use Sentry::getUser() in your views.
I have made a public function for getting, and showing a user's infraction info. When I put it on a page, it only shows what I have in the function, and not any of the other content on the page. It only shows the table headers, and none of the data. I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/AMPPS/www/classes/user.php on line 108
Also, I have other functions from the same class that work fine.
Here is the code for the function link (sorry about pastebining it, it was really long)
Your $db object is null or can't be accessed. Your line 108 error does not match up with your code you have pasted and you don't have the code where you are creating your database object to see what may be wrong there.
The error message seems to indicate that your "$db" is not set to an object. Make sure its initialized properly.
The function never initializes the $db variable. If it's a class property, it should be $this->db or self::$db. If it's a global variable, you need to put global $db; at the beginning of the function.
I register class methods for actions in my Wordpress plugin. When my method gets called by Wordpress, if I try to use the $this variable, php throws an error saying the call to $this variable is illegal outside the context of an object.
How can that be? I thought unless the method is static, you're not supposed to be able to call class methods if the class isn't instantiated! My method isn't static! What is happening?
The source code
Obviously the initialize is called from the main plugin file:
add_action('init', array('AffiliateMarketting', 'initialize'), 1);
My class looks like this:
class AffiliateMarketting
{
public function __construct()
{
// some initialization code
}
public function initialize()
{
add_action('woocommerce_before_single_product', array("AffiliateMarketting", "handleAffiliateReferral"));
}
public function handleAffiliateReferral($post)
{
$this->xxx(); // <---- offending function call
}
public function xxx()
{
}
}
The received error message is in fact Fatal error: Using $this when not in object context in <filename> on line <linenumber>.
You have to instantiate the class first. Something like this:
$affiliatemarketing = new AffiliateMarketing;
and then do the following:
add_action('init', array(&$affiliatemarketing, 'initialize'), 1);
Edit: forgot to add, your action in your method should be added like this:
add_action('woocommerce_before_single_product', array(&$this, "handleAffiliateReferral"));
You're not supposed to be. That's why you're getting an error.
I don't know exactly how you're registering the method (code would help), but probably, you're expecting Wordpress to take care of creating an instance, but that's not its role.
I found thought the Codex documented if the class name is specified using its string representation, then the add_action function will assume the call is to a static method.
On the other hand if and instance of the class is passed along then add_action will use that instance to make the method call.
Although Arman hasn't specified which php version he is using, I would assume it's probably 5.3.2 or 5.3.3. The error itself is rather similar to the one described in this question and the solution also would be to upgrade to the latest version of php 5.3.
I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();