static-methods

Call static method from a string name in PHP

天大地大妈咪最大 提交于 2020-01-02 00:55:10
问题 I need to call a static method of a class, but I only have a classname, not an instance of it. I am doing it this way. $class = new "ModelName"; $items = $class::model()->findAll(); It works on my computer, but when I move to the server, it throws an unexpected T_PAAMAYIM_NEKUDOTAYIM , so I think it actually expects model to be a variable instead of a method. PS: If it helps, it's Yii framework, so if there's another way to call the find() functions, it's ok to me. Thanks in advance 回答1: This

Memory usage when converting methods to static methods

巧了我就是萌 提交于 2020-01-01 08:37:48
问题 I started using Resharper and it indicated when a method could be made static. Would converting a few hundred methods to static methods increase the memory footprint over a large period of time? 回答1: No - Changing to static methods has no effect on memory. The first time a type is referenced (whether static or non-statically), any static members are initialized and static constructors are run. However, if you're just considering switching methods from non-static to static, this will have no

How to make static method thread safe?

不打扰是莪最后的温柔 提交于 2020-01-01 05:36:23
问题 I have written a static class which is a repository of some functions which I am calling from different class. public static class CommonStructures { public struct SendMailParameters { public string To { get; set; } public string From { get; set; } public string Subject { get; set; } public string Body { get; set; } public string Attachment { get; set; } } } public static class CommonFunctions { private static readonly object LockObj = new object(); public static bool SendMail

When is it best to use static functions in ASP.NET?

≡放荡痞女 提交于 2020-01-01 03:22:08
问题 I have been wondering, when to use static functions, and when not to in ASP.NET? What are the advantages and disadvantages in using them, in various aspects like performance, following good practices etc (and many more, whichever you feel is relevant). 回答1: Cons: threading issues (static functions don't require an instance to be called on, so it is easy to invoke them from different parts of the code and if they read/write to a shared state this state might be corrupted in a multi-threaded

Multiple Threads calling static helper method

孤者浪人 提交于 2020-01-01 03:02:47
问题 I have a web application running on Tomcat. There are several calculations that need to be done on multiple places in the web application. Can I make those calculations static helper functions? If the server has enough processor cores, can multiple calls to that static function (resulting from multiple requests to different servlets) run parallel? Or does one request have to wait until the other request finished the call? public class Helper { public static void doSomething(int arg1, int arg2

How to access a Java static method from Scala given a type alias for that class it resides in

ⅰ亾dé卋堺 提交于 2020-01-01 02:47:47
问题 Given the type-alias type Cal = java.util.Calendar how can the static getInstance method be accessed? I tried the following in Scala REPL: scala> type Cal = java.util.Calendar defined type alias Cal scala> Cal.getInstance <console>:8: error: not found: value Cal Cal.getInstance ^ scala> val Cal = java.util.Calendar <console>:7: error: object Calendar is not a value val Cal = java.util.Calendar ^ Is import java.util.{Calendar => Cal} followed by import Cal._ really my best bet? 回答1: You can't.

non-static method getActivity()

偶尔善良 提交于 2019-12-31 07:37:07
问题 I made the method "showResult" static but i have problem with : "Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();" How can resolve my problem? I will share my code. THANKS IN ADVANCE EVERYBODY! Here is my first class Fragment.java public class MyListFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener { ListView lv; ArrayList<Planet> planetList; static PlanetAdapter

non-static method getActivity()

倾然丶 夕夏残阳落幕 提交于 2019-12-31 07:37:05
问题 I made the method "showResult" static but i have problem with : "Toast.makeText(`getActivity()`.getApplication(), result2 + "\n" + "Total Amount:=" + totalAmount2 + "€", Toast.LENGTH_LONG).show();" How can resolve my problem? I will share my code. THANKS IN ADVANCE EVERYBODY! Here is my first class Fragment.java public class MyListFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener { ListView lv; ArrayList<Planet> planetList; static PlanetAdapter

How can i modify variables in static member function?

烈酒焚心 提交于 2019-12-31 05:29:13
问题 I have a code below, i want to modify class's variables in static function but there is some error. How can i fix it with "this" pointer? There is no access to "this" pointer for static members in class,on the other hand I am trying to make an access to class variables in Static member function, therefore i am looking for a way to use "this" pointer of class "me" to do it. class me { public: void X() { x = 1;} void Y() { y = 2;} static void Z() { x = 5 ; y = 10; } public: int x, y; }; int

Reflection: get invocation object in static method

狂风中的少年 提交于 2019-12-31 03:53:28
问题 Is it possible to get an object that invoked static method in this method? I have this code: class A{ static void foo(){ } } A a = new A(); a.foo(); Can I get instance a in method foo() ? 回答1: Firstly, your code isn't good as a programmer. It is because static methods are class-level methods and should be called without any instance of class. Recommended approach : class A{ static void foo(){ } } A.foo(); Can I get instance a in method foo() ? Nope, you can't. Because foo() is declared as