php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #27251 Sharing variables, functions and objects between 2 running scripts
Submitted: 2004-02-14 03:18 UTC Modified: 2013-10-27 17:28 UTC
Votes:19
Avg. Score:4.7 ± 1.0
Reproduced:15 of 17 (88.2%)
Same Version:7 (46.7%)
Same OS:8 (53.3%)
From: sven_oostenbrink at yahoo dot com Assigned: krakjoe (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2004-02-14 03:18 UTC] sven_oostenbrink at yahoo dot com
Description:
------------
I would really like to see a new feature in PHP that could put a very good use to the new object possibilities:

Being able to share objects between 2 or more running scripts. Somewhat equal to the shmop functionalities, but more precise, so that I can actually make an interface with a number of functions, variables or objects that can be called by another php script.

Normally, for each page loaded, I would have to create an object from a class, and destroy it. Next page, I again, have to create an object from the same class, use it, and destroy it.. Here I have to create and destroy all the time,  where, in (for example) a C++ program, an object is created once, and used many. a PHP object is basically create once, use once..

If objects, variables and functions could be shared (passed on) from one script to another for example, we could do the following example:

I have a database engine object, as a wrapper around the mysql functions. this object needs to be loaded for each and every php page. This object is big and heavy, and I loose quite some resources on it for all the creation db connecting, and destruction of the objects.

With the object sharing, I could start a php page (say, called core.php) that never dies, it stays in a loop with waits. this page will create one or more database objects, depending on howmany are needed at each period of time. 

These objects will immediately connect with the database (with persistent connecctions if needed). Now, when a page is loaded, and the php of that page starts running, in stead of creating the SQL object, connecting to the mysql database, etc, this script only access the interface of core.php, request a database object, gets the object, and immediately, this script will be able to access the database without all the fuzz it normally has to do.. Then, when the script is finished, it will return the object, mission completed.. The core.php has the object back, and one time object creating and destructing and database connecting has been saved.

This can be applied to alot of functionalities, and it would save off ALOT of overhead.. I dont know in how far this is possible, 

Reproduce code:
---------------
more or less pseudo code:

script.php
<?php

  // See if the core interface is available
  while(!$core=check_if_interface_available("core")){
    // Core interface not available. Start the core.php.
    exec("php core.php");
    wait 2 // wait a little to give core.php enough time.
  }

  // core interface is available,
  if(!$sql=$core->get_sql()){
    error("did not receive database object.");
  }

  $sql->use("database_name");
  $sql->query("blah blah");

  // finished, return the database object.
  $core->return_sql($sql));
?>


core.php:
<?php
  $interface_id="core";
  $interfaces=array("stop_core", "get_sql", "return_sql")
  make_interface($interface_id, $interfaces);

  // Make enough database objects.
  $sql_objects=array();
  $sql_objects[]=new sql_object();
  $sql_objects[]=new sql_object();
  $sql_objects[]=new sql_object();

  // Report the sql object that is given away as busy.
  function get_sql(){
    $sql=$sql_objects[free_sql_id];
    $sql->busy();
    return $sql
  }

  // report the sql object that was returned as available.
  function return_sql($sql){
    $sql->available();
  }

  // signal core.php to end.
  function stop_core(){
    $stop_core=TRUE;
  }

  // endless loop...
  while(!$stop_core){
    wait 1;
  }
?>

Expected result:
----------------
core.php will basically run continuously, and act like an SQL object pool where all requested php pages can ask for a database interface object.


I would think that this would save off alot of time, since objects do not need to be created for each requested page, and they dont need to be needlessly destroyed..

This would basically fill up a pretty big hole that I have seen for a long time in PHP...

Actual result:
--------------
Please let me know what you think of this idea

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-01 23:33 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: ALL +Operating System: * -PHP Version: 5CVS-2004-02-14 (dev) +PHP Version: *
 [2013-10-27 17:28 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-10-27 17:28 UTC] krakjoe@php.net
This runs contrary to the model of multi-threading in use by the interpreter, it is simply not possible while PHP is still PHP, and Zend is still Zend.

Paradoxically, there are some extensions that allow you to achieve some of the things you are talking about, gearman, pthreads, but only some; because PHP is still PHP and Zend is still Zend.

Closing the bug, thanks for your input.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Jan 20 14:01:23 2020 UTC