php - Help with static class members -


I have always used this in my classes, now I have to use a steady class and I am lost. Can anyone tell me how to use this stable members? I have tried to see some tutorials, but this is not easy to understand.

Thank you.

  class db     Private static $ example; Private static $ database_name; Private static $ database_user; Private static $ database_pass; Private static $ database_host; Private static $ database_link; Personal Function DB () {self: $ database_name = "name"; Self: $ database_use = "user"; Self: $ database_pass = "password"; Self: $ database_host = "host"; } Public stable work getInstance () {if (! Self: $ instance) {????? Auto :: $ instance = connect (); Returns self: $ database_link; } Return Self: $ example; } Function dbLink () {self: $ connect (); Returns self: $ database_link; } ...  

Edit:

In addition to this, I am really interested in using the static sections on class members who are really interested in the class Can be used outside. I believe in security but it's about it.

only for getInstance) method static.

The key is to make the constructor private, so that the class can not be made anywhere, but getInstance () ; From manual:

  & lt ;? Take a example of Php class example {// Class Private Static $ example; // a private manufacturer; Object private function stops direct creation of __construct () Icho 'I am built'; // Singleton method public static function singleton () {ifet! (Feat (self: $ example)) {$ c = __CLASS__; self :: $ example = new $ c;} return self :: $ example;} // example method public function bark () {echo 'wow!';} // users to stop public function __clone cloning () { Trigger_error ('clone not allowed'., E_USER_ERROR);}}? & Gt;  

ensuring singleton pattern That's the only thing you have - for example database connections - which can be expensive to create.

You get your example like this:

  $ MyInstance = Example: getInstance ();  

This example is a regular object - you access methods as normal (non-static):

  $ myInstance-> bark ();  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -