I am translating a website and I need to input $AppUI->_('') to make translation in system admin. But, somehow I can't use it in this function.
What am I doing wrong?
Here is my script and I hope you will help me:
public function BeginCalc()
{
if($this->getActive())
{
echo $AppUI->_('Calculating');
}
else
{
$this->_BeginCalculation();
echo "Calculation has been started";
}
}
And, when I use like this I get error Fatal error: Call to a member function _() on a non-object in. Tried to search in Stackowerflow but answers there haven't helped me in solving the problem.
$AppUI does not exists in this scope. If it's global variable, you need to import it with global keyword.
But i'd rather advise you to make $AppUI object a singleton and reference to it by AppUI::getInstance()->_('Calculating');
Related
I have read a lot of similar questions and answers but none seems to address my situation.
CLASS A
class A{
public function walk() {
...
}
public function dance() {
require 'dance.php';
}
}
Now inside of dance.php, I have
$this->walk();
And I get this error:
Fatal error: Using $this when not in object context
Please help. Is it that I cannot use $this inside a required file?
Thanks
I have testet it in a clean environment and it works fine for me.
I assume that you maybe include dance.php somewhere else in your code where you are not in object context.
Please take also a look at Possible to access $this from include()'d file in PHP class? which is maybe a duplicate question.
Just tested it, it should work (PHP 5.6).
In my dance.php is just <?php $this->walk(); and the remote method is called. The error must be somewhere else. Looks like you import the file somewhere else where you are not in object context.
Probably you should use traits. Traits enable to build logic and keep them separate to specific classes.
trait dance
{
public function dance()
{
$this->walk();
}
}
class A
{
use dance;
public function walk()
{
echo 'go forward';
}
}
(new A())->dance();
Live example: https://3v4l.org/g1Ybd
There are a number of Q's on this topic but I wasn't able to get it to work…thanks in advance for any and all help you can give.
Basically, I'm trying to do an AJAX call and it is working successfully. The call is made to a file named stores.php. Within that file, the following function is called:
function function1($ABC){
echo "…";
...
$var = function2($DEF);
… //do stuff with var
}
The trouble crops up in function2:
function function2($GHI){
global $db;
$query ='SELECT …';
… // more...
$results = $db->query($query);
return $results;
}
For the query() call to $db, I get the error: "Fatal error: Call to a member function query() on a non-object…"
I don't really understand this because I am inviting it into the function with 'global'. In some of the other Q's, people discussed some sort of scoping problem when a function is called from within another function that I didn't really understand, but I thought the point was that the function gets its predecessors scope. So I went around declaring it global wherever I could think of that would be relevant, and it didn't work.
I even re-included the script that originally created the $db variable in that function, and it still felt it was null!
NOTE: PLease keep in mind that the file, stores.php contains many functions that all successfully use "global $db" in making queries of the db.
Thanks again...
Ok, the problem was very simple, but also very specific to my page. Basically, stores.php (the file which contained the functions) was being included [via include()] to a main page. Earlier in that page, the variable $db was being set. Therefore, all the functions that were written in the stores.php page were able to access $db once it was declared with 'global'.
But when I was making the call that was failing, I was calling the function from stores.php directly--not the main page. And on that page, $db isn't ever created at all! So it was null.
SInce you are using a variable which is global, make sure that none of your functions that use global $db, will not overwrite it. Best way for making sure, if the global $db actually is object, is to use print_r($db) inside the function where that exception gets thrown.
I have set up a class with a few functions in it. I've included a seperate php file into the main one. I have a few public functions set up, all which are working. I just went to add a new one, and its throwing an error and I can't figure out why.
if(!class_exists("classBase"))
{
class classBase
{
public function printName()
{
$name = 'Test Name!';
return $name;
}
}
}
I'n my separate file that is included into this one, I am trying to call this function as I am all my other functions in the file.
<?php $this->printName(); ?>
I tried declaring the function before and after the file is included, But for whatever reason, this is throwing the error:
Fatal error: Call to undefined method classBase::printName()
I even tried copying a working function, appending a number to the function name, and calling that new function. But still throwing an error. I'm confused as to why its not working.
You need to initialize an instance of that class. In your example:
if(!class_exists("classBase"))
{
class classBase
{
public function printName()
{
$name = 'Test Name!';
return $name;
}
}
}
$myInstance = new classBase();
$myInstance->printName();
Face Palm
So damn embarassing...
I have two localhost test sites set up. I was editing the class file on the site I wasn't testing, and adding the call to the function into the site I was testing. Therefore it was throwing the error and it was correct, the method was never defined.
Just spent 45 minutes wondering what I was doing wrong...
Clearly I need to step away from the computer and go on lunch.
I`m currently working on a script, and I have the following situation.
function somnicefunction()
{
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
}
For some reason, the above breaks the function. I'm not sure why, I haven't seen much documentation in php.net regarding this, plus google returned no real results.
Does anyone have any idea ?
If you call the function more than once, you may encounter an error by trying to include the same file.
Try using require_once() instead. Other than that, there is nothing inherently 'illegal' about your example.
Your code is absolutely valid. I tested it on localhost and it works just fine. I used following piece of code:
function.php
function loadClass()
{
include_once "include.php";
new SomeExternalClass();
}
loadClass();
include.php
class SomeExternalClass {
public function __construct( ) {
echo "loads...";
}
}
Are you sure you don't have any typo there? If you aren't getting any error, it might indicate that you haven't used the function anywhere.
Try declaring the object first and then pass it as a parameter to the function:
require 'someexternalclass.php';
$somevar = new SomeExternalClass();
function somnicefunction(SomeExternalClass $somevar)
{
// Do function stuff
}
I've been implementing a certain plugin (dtabs) on my page in Wordpress but after upgrading to the latest version, I found that I now have an error the 2nd time I call the main function called dtab_list_tabs().
The way it works is, the plugin gets include_once'd but the main function is called however many times you want to place tabs in your layout. I have 2 such calls to dtab_list_tabs().
Now, the problem is, for whatever reason the developer decided to include another function directly inside dtab_list_tabs() called current_tab(). Because it's declared within a function, apparently PHP tries to redeclare it as soon as you call the parent function the 2nd time, which doesn't make any sense to me.
PHP Fatal error: Cannot redeclare current_tab() (previously declared in .../wp-content/plugins/dtabs/dtabs.php:1638) in .../wp-content/plugins/dtabs/dtabs.php on line 1638
The code for that revision is at http://plugins.svn.wordpress.org/!svn/bc/208481/dtabs/trunk/dtabs.php
What I'm trying to figure out is whether there is a way to tell PHP that yeah... it has an internal function, which is a perfectly valid PHP paradigm as far as I know, so don't redeclare it and fail.
As for the situation at hand, I have removed current_tab() as it doesn't appear to be used.
You can use function_exists() to test if a function with that name has already been defined. If you make the definition conditional ( if(something) { function foo() {...} } ) php will "evaluate" the definition only when the condition is met.
function foo() {
if ( !function_exists('bar') ) {
function bar() {
echo 'bar ';
}
}
bar();
}
foo();
foo();
see also: http://docs.php.net/functions.user-defined
(But I'd try to avoid such things all together)
You can wrap your function declaration in an if statement. Use function_exists() to see if the function has been previously declared or not.
if(!function_exists('current_tab')) {
function current_tab() {
myMagicCode();
}
}
You can try this:
if (!function_exists('my_function')) {
function my_function() {
}
}
function_exists() - Return TRUE if the given function has been defined