开发者

Is there an equivalent to C#’s Process.Start in PHP?

开发者 https://www.devze.com 2023-01-02 21:53 出处:网络
In .NET The Process开发者_StackOverflow class contains several useful properties/methods that allow developers to access process relative information.
  1. In .NET The Process开发者_StackOverflow class contains several useful properties/methods that allow developers to access process relative information. Is there any equivalent method or class in PHP?

  2. Is there any equivalent method in PHP like C# method "Process.Start()"?


1. See Program execution Functions

Except there's no concept of methods/classes/properties/namespaces in the PHP standard functions. PHP is essentially a procedural programming language with few OOP constructs and namespace support being added as a new feature as of the last major release (5.3). It's one of the reasons why people criticise it as a 'toy' language. You can access all of the PHP built-in functions all the time, no pesky namespaces to get in the way ;), just be careful of name collisions.

2. @YSJaitawat Right answer bad reference.

Click this link for the exec function documentation in the PHP manual.

Note: Also, if you're migrating from C# to PHP and looking for info the PHP manual has some surprisingly good info including user-submitted comments at the bottom of the entry where people usually post use-cases or extensions to the standard uses. It's probably the easiest language to learn because of the wealth of info to be found in the manual.


Following code snippet should do the same as "Process.Start()"

$executable = 'executable process'; exec($executable);

more on process control and execution can be found at http://php.net/manual/en/book.pcntl.php


Evan Plaice's answer covers some of the basics, however a large part of the POSIX system is all abount managing processes and communication - of course of you're not on a Unix/Linux platform then all this functionality is not available. If you are, then check the manual

However it does not attempt to address the functionality implemented by the .Net process class - which is tied closely to the Microsoft idea that the program is the GUI. For working with interactive windows then you'd have to look at the GTK or MSWindows bindings available as bolt ons.


If you want access to the stdin and stdout of the process you create, you can use:

http://be.php.net/manual/en/function.proc-open.php


You can use the COM object if (as your post implies) you're running on Windows (Note it won't work at all on Linux, but it does give you a lot that you couldn't do without it).

$com = new COM('WScript.Shell');
$com->run('Path/To/Shell/Program', 0, false);

The run command is detailed here.


In .NET The Process class contains several useful properties/methods that allow developers to access process relative information. Have you any equivalent method or class in PHP.

You probably would get more/better answers if you specified which of properties/methods you are interested about. I'm not a C# developer and I'm also not sure if this is what you're after, but what the PHP Manual has to say about connection handling is this:

When a PHP script is running normally the NORMAL state, is active. If the remote client disconnects the ABORTED state flag is turned on. A remote client disconnect is usually caused by the user hitting his STOP button. If the PHP-imposed time limit (see set_time_limit()) is hit, the TIMEOUT state flag is turned on.

You can check the connection status with... connection_status():

switch (connection_status())
{
    case CONNECTION_NORMAL:
        // ...
    break;

    case CONNECTION_ABORTED:
        // ...
    break;

    case CONNECTION_TIMEOUT:
        // ...
    break;
}

The POSIX functions (available only under POSIX systems) provide some additional information. Also, some miscellaneous functions, specifically sys_getloadavg() and the sleep functions can be useful to do, depending what you're looking for.

Have any equivalent method in PHP like C# method "Process.Start()".

There are several Program Execution functions, specifically:

  • exec()
  • passthru()
  • proc_open()
  • shell_exec() - see also the backtick operator
  • system()

Under Windows you can also use the COM class to open up the command prompt:

$cmd = new COM('WScript.Shell');

Also, if you want to be safe from code injection attacks don't forget that any user supplied input should be escaped with either escapeshellarg() or escapeshellcmd().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号