0

Possible Duplicate:
Redefine Built in PHP Functions

How can I overwrite a built-in PHP function?

I want to overwrite mysql_connection function, function should check if the connection is already opened, just return that connection or else request for a new connection.

I am working with some legacy system, which has 1000's of connections on each page. This reduces the performance of the website and potentially causes bad memory usage.

I want to rework this in such a way that whenever sql connection request is made, system has to check whether the connection is already there or not.

I can't go and change in all the places to replace the connection to common object because system is not written in MVC.

Anybody can help me?

Verv
  • 2,385
  • 2
  • 23
  • 37
Sahal
  • 4,046
  • 15
  • 42
  • 68
  • 6
    From the [documentation](http://php.net/manual/en/function.mysql-connect.php): *If a second call is made to `mysql_connect()` with the same arguments, **no new link will be established,** but instead, the link identifier of the already opened link will be returned.* Do you explicitly request a new link at every call? Or do you change the parameters? If not, there is no need to overwrite the function. `mysql_connect` already reuses the connection. – Felix Kling Jun 27 '11 at 09:34
  • 3
    you might be looking for [`mysql_pconnect`](http://de3.php.net/manual/en/function.mysql-pconnect.php). Also keep in mind that you can configure the amount of connections mysql is allowed to use through the various [runtime configurations](http://de3.php.net/manual/en/mysql.configuration.php) – Gordon Jun 27 '11 at 09:44
  • You don't need to have an MVC architectured app in order to use classes! please stop confounding the two things! – Damien Pirsy Jun 27 '11 at 09:49
  • Yes thats true, But i don't have time to create new DBUtility class to put the connect function in the common place. – Sahal Jun 27 '11 at 09:51
  • tip: next time you ask a question, put all these things you cannot do into the question right away. Then you dont have to complain over the reasonable advice you have been given, because then they would have told you right from the start that there is no hope. – Gordon Jun 27 '11 at 10:07

1 Answers1

2

For the literal answer to your question, see the duplicate link: Redefine Built in PHP Functions

However, overwriting the native mysql_family of functions is going to be a futile undertaking. You would have to re-implement their functionality in PHP which is probably going to be impossible - they are part of the mysql extension, and I assume are written in C.

I think the only way for you to go is to define an alternative class or set of functions, and then do a search + replace across the legacy system, replacing all mysql_ calls by calls to your new ones.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I can't do that. As i said there are so many connections. I can not replace just 'mysql_connect' function. Coz all connections are different. Connections are not stored anywhere. No object used. – Sahal Jun 27 '11 at 09:43
  • @Sahal why can you not do a search and replace? – Pekka Jun 27 '11 at 09:46
  • Can you tell me what you mean by search and replace ? what You will replace ? place where "mysql_function" i am calling ? – Sahal Jun 27 '11 at 09:48
  • I am not using any common object. Each call act as new. i can not check like if($connection_exists) return connection. – Sahal Jun 27 '11 at 09:49
  • @Sahal: Text search and replace. I hope you also read my comment. What you are trying to do might not have any effect. – Felix Kling Jun 27 '11 at 09:50
  • @Sahal you will not be able to overwrite `mysql_connect()`. So the suggestion is to write your own function (named, e.g. `my_mysql_connect()`) that does what you need, and replacing every `mysql_connect()` by `my_mysql_connect()` in the code. But the more comments come up, the more it looks like that might not solve your problem after all anyway. – Pekka Jun 27 '11 at 09:52
  • @ Felix Kling , i know you are taking about text replace :) .. But what text and what is to be replaced ? – Sahal Jun 27 '11 at 09:52