views:

393

answers:

6
  1. In .NET The Process 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()"?

+4  A: 

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

YSJaitawat
A: 

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

Jordan
+3  A: 

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.

Evan Plaice
I believe that many of them are in the current release. Namespaces in 5.3, and I've used constructors in my code.
DeadMG
@DeadMG updated the answer for accuracy and to make it more 'future proof'. When I said 'constructs' I didn't mean 'constructors'. By saying that PHP is has few 'constructs' I'm simply stating that OOP is there but it's not fully-featured in the manner that a Java/C#/Python programmer would expect. OOP in PHP is pretty minimal compared to most languages that claim to support OOP.
Evan Plaice
@DeadMG "I believe that many of them are in the current release" yes, they're there but they're not used in the standard library. To use 'exec' you don't type something like 'process::exec()' because namespaces and OOP aren't used in the standard library at all. Essentially, all of the built-in functions of PHP live in the main namespace. That last sentence is enough to make a lot of developers cringe but to fix it, the developers of PHP would have to re-map all of the thousands of built-in functions.
Evan Plaice
I didn't mean literally constructs as constructors either (that hadn't occurred to me), I meant that I'd used the construct of having a constructor in an object.
DeadMG
@DeadMG ok, scratch that. The point I was trying to illustrate in my answer was. If you're accustomed to C# and you use PHP you're up for a surprise because PHP has no semblance of a 'standard library' like most languages do; and OOP/namespaces are there but not in the same way they would be in a strict structure of classes/namespaces that exist in C#. PHP is a 'procedural language with OOP features' while C# is a 'strictly OOP from the ground up'.
Evan Plaice
God, why would you migrate from C# to PHP?
David Lively
@David There are a lot of good reasons one may use PHP over C#. It's common, has a massive developer base (web specific developers), is dynamically typed, easy to use, and there are a lot of really useful and stable frameworks built using it. Personally, I don't see the advantage of using a statically typed language to build websites. For what I could write in 1000 lines of C# I could write in 400 of PHP or 250 of python. When you send code across the wire, number of characters *can* take precedence over number of clock cycles to execute. Those reasons and more make php a viable alternative.
Evan Plaice
I'm sorry : send CODE across the wire? If you don't see the advantage of a statically-typed language, you haven't spent enough time debugging someone else's code, or chasing case errors in a 400k line application. There's a reason we traded BASIC in for C 30 years ago.
David Lively
Also, you contradict yourself. Character count in c# is irrelevant, since it's compiled. PHP, on the other hand, natively requires every page and every include to be reparsed for every request. Even opcode caches like Zend don't adequately address this fundamental issue with the design of page-based processors.
David Lively
@David Oops, forgot to consider caching of bytecode. Either way, I'm not bashing C#, I love to write code in it and I agree that it is a lot easier to track errors in a massive application as you described. I'm just saying that, for general web development (small scale) C# and/or a statically compiled language is overkill. It's also worth mentioning that .NET is a windows/mac only platform with mono being perpetually a few steps behind .NET in development. The fact that a lot of HTTP servers are running on *nix and .NET is a pseudo-open platform may make PHP a more attractive choice.
Evan Plaice
@David I'm not saying that PHP is a better language than PHP in a nuts-and-bolts comparison. I'm saying that there are (technical/political) reasons not to disregard PHP as a choice for web development. Even though the .NET VM and BCL fall under ECMA standards, WPF and Silverlight don't. That fact is unsettling to a lot of developers.
Evan Plaice
@David One last note, to say that there is no server-side caching in PHP is just jibberish. Sure, dynamically generally content isn't automatically cached in PHP. But neither is it in ASP without additional steps.
Evan Plaice
+1  A: 

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 do RTFM

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.

symcbean
+1 Good point. I wouldn't limit the capabilities of process management to just POSIX. There's a surprising amount you can do along those lines in .NET on windows too. The problem is, PHP is very limited in that aspect. To do anything more complex than basic open/close/change priority, you'd have to execute basic shell commands and parse the sdtin stdout stderr which can be an extreme pain. It's do-able, just not easy.
Evan Plaice
A: 

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.

ircmaxell
A: 

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:

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().

Alix Axel