A very basic object-oriented problem, asking for advice in a class (actually the controller class of thinkPHP), there are many methods (not all methods) that will use some statements, I want to integrate it into one place (such as a method ?) Call it everywhere to learn how to write the sample code that has been pasted out. (it is a very basic object-oriented problem.
In a class (actually the controller class of thinkPHP), there are many methods (not all methods) that will use some statements, I want to integrate it into one place (such as a method ?) Call it everywhere to learn how to write
Sample Code (incorrect)
Class AbcAction extends BbcAction {
Public function init (){
$ Model = M ("table1 ");
$ A = $ model-> where ("id = 1")-> getField ("id"); // $ a = 1
$ This-> assign ("a", $ );
}
Public function test (){
$ This-> init ()
Echo $;
$ Model-> find (1 );
// I hope the above $ a and $ model can be used here (in fact, this is an error because this is the internal variable of init?
// The value passed by init to the foreground. it seems that this value is acceptable.
}
}
The problem is very small, please don't hesitate to enlighten me (I am going to use it now, it's too late to go to the system to learn object-oriented...) the variables in the object-oriented method are shared:
------ Solution --------------------
Can I set two attributes? $ This-> model = $ model; $ this-> a = $ a; you can call this class, but you must call the init () method first.
------ Solution --------------------
Class AbcAction extends BbcAction {
Public $ model;
Public $;
Public function init (){
$ This-> model = M ("table1 ");
$ This-> a = $ this-> model-> where ("id = 1")-> getField ("id"); // $ a = 1
$ This-> assign ("a", $ this-> );
}
Public function test (){
$ This-> init (); // if the construct is equivalent to _ construct, it will be executed when AbcAction is instantiated.
Echo $ this->;
$ Model-> find (1 );
// I hope the above $ a and $ model can be used here (in fact, this is an error because this is the internal variable of init?
// The value passed by init to the foreground. it seems that this value is acceptable.
}
}
------ Solution --------------------
If the init method is executed by default, and the test method must use the intermediate result generated by the init method
The intermediate results produced by the init method must at least be global in the class.
Class AbcAction extends BbcAction {
Public function init (){
$ This-> model = M ("table1 ");
$ This-> a = $ model-> where ("id = 1")-> getField ("id"); // $ a = 1
$ This-> assign ("a", $ this-> );
}
Public function test (){
// $ This-> init ()
Echo $ this->;
$ This-> model-> find (1 );
// I hope the above $ a and $ model can be used here (in fact, this is an error because this is the internal variable of init?
// The value passed by init to the foreground. it seems that this value is acceptable.
}
}
------ Solution --------------------
Yes?
The variables in a function (method) are private. you cannot change them to public. how can this problem be solved?
I get money from your wallet. would you like it?
------ Solution --------------------
Public function test ()
{
// Encapsulate a curl method here to directly request the following getData () method
// $ Url = 'http: // 192.168.1.1/module/controller/getData ()';
Curl_setopt ($ ch, CURLOPT_URL, $ url );
}
Public function getData ()
{
// You can put all the data in this method.
Echo json_encode ($ data );
}
I don't know if this is more suitable for your needs ....
------ Solution --------------------
Of course. but this is not the TP style.
ThinkPHP advocates one case, so a simple method of loading a data table instance using an M function
If you don't like this method, you don't have to use it any more.
If you build your nest within his framework, serious coordination problems may occur in the future.
After all, you cannot modify the kernel at will.
Reference:
Quote: reference:
Yes?
The variables in a function (method) are private. you cannot change them to public. how can this problem be solved?
I get money from your wallet. would you like it?
Is there a way to reference this block of code to a shared place without using the "call shared function" method? In this way, you can directly use the content in the code.
In this case, it should not be called object-oriented, but simply reused.