@@ -87,14 +87,22 @@ public function connect($suppress_error = false)
8787 * @return boolean True if connected, false otherwise
8888 */
8989 public function ping ()
90+ {
91+ $ this ->ensureConnection ();
92+ return $ this ->getConnection ()->ping ();
93+ }
94+
95+ /**
96+ * Establishes a connection if needed
97+ * @throws ConnectionException
98+ */
99+ private function ensureConnection ()
90100 {
91101 try {
92102 $ this ->getConnection ();
93103 } catch (ConnectionException $ e ) {
94104 $ this ->connect ();
95105 }
96-
97- return $ this ->getConnection ()->ping ();
98106 }
99107
100108 /**
@@ -117,7 +125,7 @@ public function close()
117125 */
118126 public function query ($ query )
119127 {
120- $ this ->ping ();
128+ $ this ->ensureConnection ();
121129
122130 $ resource = $ this ->getConnection ()->query ($ query );
123131
@@ -146,7 +154,7 @@ public function multiQuery(Array $queue)
146154 throw new SphinxQLException ('The Queue is empty. ' );
147155 }
148156
149- $ this ->ping ();
157+ $ this ->ensureConnection ();
150158
151159 // HHVM bug (2015/07/07, HipHop VM 3.8.0-dev (rel)): $mysqli->error and $mysqli->errno aren't set
152160 if (!$ this ->getConnection ()->multi_query (implode ('; ' , $ queue ))) {
@@ -168,10 +176,7 @@ public function multiQuery(Array $queue)
168176 */
169177 public function escape ($ value )
170178 {
171- if ($ this ->connection === null ) {
172- // this function is called a lot, making ping a significant cost if called every time
173- $ this ->ping ();
174- }
179+ $ this ->ensureConnection ();
175180
176181 if (($ value = $ this ->getConnection ()->real_escape_string ((string ) $ value )) === false ) {
177182 // @codeCoverageIgnoreStart
0 commit comments