In java and php, many Singleton modes are used in java and php. what are their differences? I compared each other last night and made a small conclusion, then I wrote a small java Singleton and php Singleton. I. java Singleton completed Simple Authentication for user login. classUser {privateStringnamenull; username privateStringp is in java and php, differences between Singleton mode
Many Singleton modes are used in java and php. what are their differences? I compared each other last night and made a small summary. then I wrote a small java Singleton and a php Singleton.
I. java Singleton
// Complete Simple Authentication for User login class User {private String name = null; // User name private String passwd = null; // password private static User ins = new User ("constants can be input here, for example," chunge ". at this time, the constructor must have corresponding parameters. However, it cannot be assigned an external value in the static initialization function "); // A single instance object // code block that initializes the user name and password {this. name = "chunge"; this. passwd = "111111";} // private constructor private User (String n) {this. name = n ;}// obtain the singleton object public static User getIns () {return ins ;}// determine whether the User information is correct public void checkUserInfo (String name, String passwd) {if (name = this. name & passwd = this. passwd) {System. out. println ("welcome" + this. name); System. exit (1);} // The user password does not match System. out. println ("illegal logon"); System. exit (1) ;}}; public class oop {public static void main (String args []) {User U = User. getIns (); // instantiate object U. checkUserInfo ("chunge", "111111 ");}};
II. php Singleton
Config_file = ROOT. "config/global_config.php"; // value $ this-> WEB_CONFIG = $ WEB_CONFIG;} // disable the clone function private function _ clone () {return false ;} // Singleton mode public static function getIns () {if (! (Self: $ ins instanceof self) {// if no self: $ ins = new self ();} return self: $ ins ;} /// dynamic output configuration information public function configGet ($ key) {if (array_key_exists ($ key, $ this-> WEB_CONFIG )) {return $ this-> WEB_CONFIG [$ key];} else {echo "this configuration information does not exist ";}} // dynamically add the configuration information input key to the public function configSet ($ key, $ value) {// truly store $ str = file_get_contents ($ this-> config_file ); // var_dump ($ str); $ tmp = $ this-> configGet ($ key); $ str = str_replace ($ tmp, $ value, $ Str); file_put_contents ($ this-> config_file, $ str); // file_put_contents ("ROOT1.php", $ str );} /// dynamic output configuration information public function _ get ($ key) {if (array_key_exists ($ key, $ this-> WEB_CONFIG )) {return $ this-> WEB_CONFIG [$ key];} else {echo "this configuration information does not exist" ;}// dynamically add the configuration information public function _ set ($ key, $ value) {// It is only a temporary configuration and is not actually saved to $ this-> WEB_CONFIG [$ key] = $ value ;}}?>
Summary:
/** Note: class name, User, Singleton variable, ins, differences between java and php, Singleton mode 1. class can be used in Singleton mode, the same as php 2. java Singleton mode variable declaration: private/public static class name object name = new class name (); example: private static User ins = new User (); // The new php Singleton mode variable declaration has been performed: public/private static object name = null; // It is just a declaration and no new operation has been performed, for example: public static $ ins = null; 3. java obtains the singleton object method public static class name method name () {return object name just defined} example: public static User getIns () {return ins; // note that this cannot be used here. but in php, you can use self: get} php to get this Singleton object method public function getIns () {if (self: $ ins = null | (! Self: $ ins instanceof self) {self: $ ins = new self ();} return self: $ ins;} 4. in java, if the Singleton mode is used to initialize certain variables in the class, the getIns () method is static. if the variable is not static, the variable cannot be instantiated. Even if the constructor needs to pass parameters, I personally think that you can directly use the code block to set the initial values in php. in Singleton mode, you can assign values to the initial values, such as public function getIns ($ can1, $ can2 .......) {if (self ::$ ins = null | (! Self: $ ins instanceof self) {self: $ ins = new self ($ can1, $ can2 ........); //....... represents an unlimited number} return self: $ ins;} This is only required to input the corresponding number of parameters to the class constructor. **/