简体   繁体   English

PHP中的Javascript(Node.js)

[英]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? 是否有类似PHP.js的项目,但有相反的方向来提供JS类和函数的PHP实现? In particular Date, RegEx, String classes? 特别是Date,RegEx,String类?

I found this class for String but I am looking for a more complete collection. 我为String找到了此类 ,但是我正在寻找更完整的集合。

My consern is not about using or not using such thing, I just need such thing. 我的考虑不是关于使用或不使用这种东西,我只需要这种东西。

Today I found jsphp . 今天我找到了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+. 适用于PHP的JavaScript(jsphp)是适用于PHP 5.3+的ECMA 262标准(JavaScript 8.5.1)的伪实现。 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. 它允许您使用标准API和该语言提供的动态属性,像使用JavaScript一样,用PHP编写代码,例如原型继承和链接,一流功能以及其他面向对象的功能。 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. 它包括JSBoolean,JSNumber,JSString,JSObject,JSArray,JSFunction,JSRegExp,JSDate,JSError和JSMath,以及全局帮助程序函数,例如parseInt或isNaN。 The syntax in jsphp is very similar to a native implementation, although adapted to the syntax of PHP. jsphp中的语法与本机实现非常相似,尽管适用于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. Javascript对于正则表达式具有特殊的语法,这是PHP不能接受的。

Similar how "object" methods in javascript are invoked. 类似于在javascript中调用“对象”方法的方式。 And the scope of variables is different. 并且变量的范围是不同的。 So this would not work. 所以这行不通。

Instead use the PHP functions. 而是使用PHP函数。 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. 或者使用许多可用的库之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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