I've done a search into stackoverflow but couldn't find anything.
Actually I'm working into recodding a "dirty" code done by a dev in a CakePHP 2.3 framework.
I'm not a dev myself, i'm more like a Swiss knife, i do some php but this is usually not my daily task.
:)
Let's go to the facts, i've got a controller with functions, and some functions under these functions...
Ex :
tool_controller.php
function addSomething(){
print_r($this->loadModel("db")); //it works // returns 1
function anotherFunction(){
print_r($this->loadModel("db")); //returns "Using $this when not in object context"
}
}
I'm a bit lost, searched in CakePHP docs but couldn't find anything either.
Can someone please help ?
thanks
This code would most probably be better written like this:-
public function addSomething(){
print_r($this->loadModel("db"));
$this->_anotherFunction();
}
protected function _anotherFunction(){
print_r($this->loadModel("db"));
}
I doubt you really need to nest functions for what you want to achieve. The above code should be simpler to read and is more obvious what context $this refers to.
OK people, i've solved my problem, as I told before the main function is an action after a submit, then I had a loop inside this function to treat the datas.
As i'm working in CakePHP and using the integrated functions (in this case the function "loadModel" in the "Controller" class), i had to get this function working in the nested function.
The solution was the following :
function addSomething(){
my_code_here
function anotherFunction(){
$controller = new Controller; // Had to redeclare the class
print_r($controller->loadModel("db")); // Works just fine
}
}
Thanks to all for your submissions.
Kind regards folks ;)
After that many of you suggest me to not to nest functions, I've decided to follow your advice and I've putted outside my main function, after I call back my function.
Public Function loopAction(){
//Code here
}
Public Function addsomething(){
//Code here
$this->loopAction();
}
It works perfectly and even better than before, thanks to all.
Related
In my screenshot below you can see I have a list of functions that run a routine, fairly in-depth routine.
Previously, I have ben repeating this routine in multiple classes, but now I would like to consolidate those multiple classes into one class and execute only one function, by passing a variable into that function to determine the output to return.
I know how to pass the variable into "one" function, but how can I pass the variable ($this_id) into my multiple functions below? Basically, whatever $this_id is from get_output($this_id); I want that same variable value to be carried over into the other $this_id functions. See screenshot...
I searched online and all answers I've seen show how to do this in a non static way, but I'm only familiar with calling things statically, really. I tried the obj way, but couldn't get it to work.
Example, execution...
$header = 'CustomTheme_output';
$header::get_output('header');
(please disregard any lose code, the code is what I have so far from trying multiple ways. private $id and __construct are from the online solutions I have been trying)
Could you please clue me in on how I can correctly achieve this? I would be sooo happy to get rid of all the repetitive code, folders and files I have! - Thanks!
Either you pass it directly into each method call:
public function foo($this_id) {
$this->bar($this_id);
}
Or you make it a class attribute, and simply ACCESS it from the various methods:
public function foo($this_id) {
$this->id = $this_id;
$this->bar();
}
public function bar() {
do_something($this->id);
}
I would like to have shared function for all extended controllers which print give json object as response.
I placed this shared function into the "AppContoller.php" as static function.
Problem is that i cannot access to this-> in AppController.
Question is:
How can i solve it?
Here is the code of the function:
public static function printJsonOutput($responseData)
{
$this->RequestHandler->respondAs('json');
$this->autoRender = false;
echo json_encode($responseData);
}
Thanks for any help.
Read the book about json and XML views.
What you want to do is explained there in great detail, with code examples and shown the right way to do it.
What you do is wrong and like #Nunser already pointed out you don't have access to $this in a static function. You might want to do some OOP tutorial for php first?
i am newer to cakephp. i am trying to write a function which not regarding to view(function). but when i call this function resultCall to undefined function. my code is below
public function records(){
$totalrec = $this->names->find('count');
$pages = ceil($totalrec/$limit);
return $pages;
}
and call it as
$rowsr = records();
please help
Where are you defining and using this function?
If you want to use it absolutely everywhere, define it in the bootstrap-file in config. But be aware that this heavily violates MVC.
Looking at the function, I would guess that you probably want to add this function to the names model. Than you can call it from the Controller this way: $this->Names->records() and in the model this way: $this->records().
actually i was searching for this.
$this->records();
this is working.
Thanks all friends.
Hi I'm a bit of a newbie to OOP, i just have a quick question: say I have a function in a class declared as
class House
{
public static function hasAlcohol()
{
// Do Something
}
}
I know i can call this as
House::hasAlcohol()
However, i would also like to know if its okay with coding standards and PHP and if it would be error free to call hasAlcohol() from an instance of house (i tried it and got no errors), for example
$house = new House();
$house->hasAlcohol();
As this has caused several problems for me in the past: Yes, it is valid code. Should you do it? No. It gives the impression that the call is non-static and will most likely cause grief for people working on your code later on. There is no reason to make your code ambiguous.
This used to be possible, but the latest versions of PHP will throw an error, if I remember correctly. You should call static functions statically. You can do $house::hasAlcohol() though.
This used to be possible, but the latest versions of PHP will throw an error, if I remember correctly. You should call static functions statically. You can do $house::hasAlcohol() though.
On a side note, should hasAlcohol really be static? From the name it appears it should be an instance method.
A more recommended pattern if you need constant access to a method is to use a static constructor and get an instance (even if it's a "blank" or "empty") instance to that class. So in the example you've shown, it might be better to have a method like this:
class House
{
public function instance()
{
return new House;
}
public function hasAlcohol()
{
// Do Something
}
}
Then if you ever needed to make a call to "hasAlcohol()" where you don't need an instance for any other purpose, you can do a one-off like so:
House::instance()->hasAlcohol();
or you can instantiate it like in your example:
$house = new House;
$house->hasAlcohol();
or, better yet, use your new factory method:
$house = House::instance();
$house->hasAlcohol();
I am trying to learn OO and classes and all that good stuff in PHP, I am finally learning the sytax good enough to use it some and I am curious if there is any benefit of starting a new object instead of just using static methods...let me show some code for what I mean...
<?PHP
test class
{
public function cool()
{
retunr true;
}
}
//Then calling it like this
$test = new test();
$test->cool();
?>
OR
<?PHP
test class
{
public static function cool()
{
retunr true;
}
}
//Then calling it like this
test::cool();
?>
I realize this is the most basic example imaginable and the answer probably depends on the situation but maybe you can help me understand a little better
For your example, it is better to use a static function, but most situations will not be so simple. A good rule of thumb to start with is that if a method doesn't use the $this variable, then it should be made static.
Think of classes like 'blueprints' to an object. you want to use the static method when it is a general function that could apply to anywhere, and use methods when you want to reference that specific object.
Here is an article that discusses differences in performance between these concepts:
http://www.webhostingtalk.com/showthread.php?t=538076.
Basically there isn't any major difference in performance, so then the choice is made based on your design.
If you are going to create an object several times, then obviously a class makes sense.
If you are creating a utility function that isn't tied to a particular object, then create a static function.