简体   繁体   中英

Javascript (Node.js) in PHP

Is there any project like PHP.js but in vice direction to provide PHP implementation of JS classes and functions? In particular Date, RegEx, String classes?

I found this class for String but I am looking for a more complete collection.

My consern is not about using or not using such thing, I just need such thing.

Today I found jsphp . It seems promising.

JavaScript for PHP (jsphp) is a pseudo-implementation of the ECMA 262 standard (JavaScript 8.5.1) for PHP 5.3+. It allows you to write code in PHP as if it were JavaScript, using the standard API and the dynamic attributes available in the language, such as prototype inheritence and chaining, first-class functions, and other object-orientated features. It includes JSBoolean, JSNumber, JSString, JSObject, JSArray, JSFunction, JSRegExp, JSDate, JSError and JSMath, as well as the global helper functions, such as parseInt or isNaN. The syntax in jsphp is very similar to a native implementation, although adapted to the syntax of PHP. For example, the variables are prepended by a "$", the Object access opertaor is "->", and Strings are concatenated using a ".".

  $myString = new JSString( 'Hello World' );

  $myString = $myString->split( '' )->reverse()->join( '' );

  print( 'Reversed: ' . $myString ); // dlroW olleH

There is not - and there can not be - somehting like you ask for.

Javascript has a special syntax for regular expressions, something PHP could not take.

Similar how "object" methods in javascript are invoked. And the scope of variables is different. So this would not work.

Instead use the PHP functions. If they are not complete or useable enough for you, wrap them into objects so you can create an interface you like. Or use one of the many libraries that are available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM