- [SOLVED] Can we fully replace PHP with Node.js??
- Often have questions like this?
- Include it
- Quick start
- PHP classes fragments
- Features translate php functions
- PHP to JavaScript Converter Online Full Source Code
- Download Source Code
- Supported Functionality of PHP to JavaScript Converter Online
- Planned Features to be Added in Future
- Limitations of PHP to JavaScript Converter
- Features Not Supported
- How to Use PHP to JavaScript Converter?
- Use auto converter
- PHPStorm
- Example PHP Code
- Above PHP Code is Converted to this JavaScript Code
- Related posts:
[SOLVED] Can we fully replace PHP with Node.js??
Can you tell me which one is better, faster, efficient and easier??
Node JS has useful features like multi threading and so on but those using shared hosting servers can’t use node so they use PHP. I am a freelance developer and since majority still prefer shared hosting. I can’t let go of PHP in my skillset but then it depends on what you want to do. Organizing php files can get messy real quick on a large scale project bcos of problems like global variables. Most problems of PHO can be solved using franeworks like Laravel. So your question: Which is better? depends faster? node i guess efficient? depends easier? definitely php Edit: Most shared hosting servers don’t support console so you might have to use php to run node
So if i make conclusion, NodeJS is faster because it has the multi-threading, and NodeJS is also said to be better than php, as in general. But if we see, which is easy and has lesser code, then mostly of us will say its php, and php has the in-build function to work with database, but js does not have that, js can have the Node.js framework to work with database, i also come to know about that we have Laravel framework for php which is very good choice. See, i did this comparison because my teacher asked me that u should now use database in ur websites, she said and showed me a code of php, also it was me that didn’t like php because writing «
quot; again and again, i like js very much, i think its very beautiful and easy. I also found this lesson on sololearn: https://www.sololearn.com/learn/9660 Also i thanks everyone for their contribution in giving me answers.
Nope PHP cannot be fully replaced. Yes node js is better but in some cases PHP is still very helpful and better choice like making single forms and small projects. Since PHP supports Facebook and after PHP 7 it has a good future.
We cant decided which one is faster better and easier. It depends on the project you are using them in. I love php and node. Js.
No, for a few reasons: 1: A lot of legacy projects use PHP. Replacing them would be WAY not worth it. 2: Despite me being a NodeJS fanboy, I gotta admit PHP really isn’t THAT bad. 3: Programming languages — especially those as mature as JS and PHP — are *tools*, not «yes or no» things. Also, to clarify, NodeJS does NOT have multithreading. It’s JavaScript, built on the same basic engine (the V8 engine) as front-end JS, and while it does take advantage of the event loop, calling it multi-threaded is just plain wrong. I’m also not entirely sure what he means by «shared hosting servers can’t use Node». This sounds more like a particular shared hosting platform *chose* not to support NodeJS; not that shared hosting platforms in general cannot support it.
We know NodeJS coding is neater and a bit easier than php. However, php handles huge projects with ease, and it is secure if is coded properly. Also, php almost like C in syntax coding, because it was created from C itself. So, don’t let the php $variable puts you off, to be a real programmer do not fancy a programming language over the other.
Often have questions like this?
Learn more efficiently, for free:
Include it
When you will add your translated from php to javascript code in the your web page, you required php.js file. This file contains some standart php functions. Missing standart php functions you wil write — I mean, it no problem. (May be exclude preg_* functions).
Quick start
You can to convert php class code or php function code to the javascript code.
In the green blocks helpful featurtes this of the translator, in the red vlocks — a bugs and problems.
PHP classes fragments
Replace ‘extends ParentClass’ to the ‘extend(ParentClass, YourClass)’. The function extend defined in the php.js
Replace ‘static function foo’ to the ‘YourClass.foo = function‘
Replace ‘private/public/protected function foo‘ to the ‘YourClass.prototype.foo = function . ‘
Replace ‘parent::foo(arg1, arg2, arg3)’ to the ‘YourClass.superclass.__constructor.call(this, $a, $b, $c);’
Replace ‘self’ to the ‘YourClass’
You can translate a code one PHP class from one launch
No support magic methods as ‘__get, __set’ you can translate your algorithm, use javascript defineProperty
No support translation combined arrays, like array(1,2’foo’ => ‘bar’), try use in your code array(‘foo’ => ‘bar’, ‘one’ => ‘two’, [‘co’ => ‘kigo’]), and array(‘s’,’k’,’d’)
No use array in your code, use [], it reliable
Features translate php functions
All variables from php function code move to the begin of the javascript function, and prepend keyword var
Extract variables from string, for example «Hello, $name !» change to the «Hello » + $name + $user[‘surname’] + «!»;
Replace string concatenation from ‘.’ to ‘+’.
Replace line break in the string php variable to the concatenation several string javascript variables
Replace definion associative array, like «[‘key’ => ‘value’]» to the «». Bad support ‘array()’ construction. Use ‘[]’ in the your php code.
You can use ‘$arr = [];//<>‘ if you are sure, what $arr will is associative array. When it output will ‘$arr = ;’. The function __php2js_push__ define in the file php.js
Replace ‘$arr[] = $val;’ to the ‘$arr.push($val);’
If argument original php function is not a reference, in the converted javascript code appear code ‘$b = __php2js_clone_argument__($b);’ Why?
In javascript every function argument type of array is reference. It behavior equivalent argument of the php function, like as you wrote ‘function foo(array &$arr)<. >‘
If original php code containts argument of the function with default value, in the translated javascript code appear code ‘$b = String($b) == «undefined» ? 10 : $b;’
The file php.js containts several standart PHP functions and it will be continue.
Php functions containing arguments as reference, and also containts arguments with default values correctly converted to javascript code only when you translate php class code to the javascript code
If you like write ‘function foo(&$a = 1, &$b = ‘bar’, &$c = 10.2, &$d)’ your code will be translated incorrect. You can replace this arguments to the object like and in function body read and write value of property val. In outer code, before call your function you must also write and read property val, but not variable simple type.
PHP to JavaScript Converter Online Full Source Code
Do you need a tool that can convert PHP code to JavaScript online?
In this tutorial, I’ll guide you step-by-step on how to create a PHP to JavaScript Converter. You can easily download the complete source code of this project and try it yourself on your computer.
Download Source Code
Supported Functionality of PHP to JavaScript Converter Online
- Namespaces, use
- Class, abstract class
- extends and interfaces
- constants and define
- Exceptions and catch
- continue, break
- anonymous classes
- magic constants
- list()
- magic methods __get __set and __call (only in ES6 see Proxy in compatibility table)
- private methods and properties (only in ES6 see WeakMap in compatibility table)
Planned Features to be Added in Future
Limitations of PHP to JavaScript Converter
There are many differences between PHP and JS. For example, PHP has associate arrays but JS does not. For that reason, you can use jsphp\JsArray which gives you the same functionality as the built-in JS Array.
In JS you have an object which is similar to PHP arrays, but there is different ordering. Also, it is not working with built-in PHP functions for manipulating arrays. So if you need this object, which works with foreach loop, then you can use jsphp\JsObject. This object has the same functionality as JsObject. But if you want to extend it, your extended object can’t have public or protected members, just use it, but not declare it.
If you need something like an associate array you can also use jsphp\HashArray
You can’t define class constant and static properties with the same name in JS will be overridden.
Features Not Supported
How to Use PHP to JavaScript Converter?
$parser = (new \PhpParser\ParserFactory())->create(\PhpParser\ParserFactory::PREFER_PHP7); $jsPrinter = new \phptojs\JsPrinter\JsPrinter(); $phpCode = file_get_contents('path/to/phpCode'); $stmts = $parser->parse($phpCode); $jsCode = $jsPrinter->jsPrint($stmts);
Use auto converter
You can create a file watcher for auto-generation of JS script from your PHP code whenever it is saved.
PHPStorm
- File type: PHP
- Scope: Create new scope to your php scripts to convert
- Program: chose yor location to php.exe
- Arguments:
- -f
- $ProjectFileDir$/../PHP-to-Javascript/bin/phpstormWatcher.php
- $FileName$
- $ProjectFileDir$/phpJs php scripts to generate
- $ProjectFileDir$/public/js/phpjs output directory
- [-p] enable support of private properties and method. If is disabled, all private fields is converted as public
Example PHP Code
interface FooInt < function fooIntFunc1($a, $b = 5); >abstract class FooAbs implements FooInt < abstract function fooAbsFunc1($a, $b); function fooAbsFunc2($a, $b)< return $a + $b + 10; >> class FooParent extends FooAbs< public $foo = 5; public function __construct() < $this->foo=5; > function fooAbsFunc1($a, $b) < parent::fooAbsFunc2(1,5); return $a + $b; >function fooIntFunc1($a, $b = 5) < return $a + $b + 5; >public static function fooStatic() < return 10; >> class FooChild extends FooParent < public $foo = 6; function fooIntFunc1($a, $b = 5)< return $a + $b; >function testParent()< assert_($this->fooIntFunc1(5, 5), 10, 'testParent 1'); assert_(parent::fooIntFunc1(5, 5), 15, 'testParent 2'); > > $fooParent = new FooParent(); $fooChild = new FooChild(); assert_($fooParent instanceof FooParent, true, 'fooParent instanceof FooParent'); assert_($fooParent instanceof FooInt, true, 'fooParent instanceof FooInt'); assert_($fooChild instanceof FooChild, true, 'fooChild instanceof FooChild'); assert_($fooChild instanceof FooParent, true, 'fooChild instanceof FooParent'); assert_($fooChild instanceof FooAbs, true, 'fooChild instanceof FooAbs'); assert_($fooChild instanceof FooInt, true, 'fooChild instanceof FooInt'); assert_(FooChild::fooStatic(),10, "FooChild::fooStatic()"); $fooChild->testParent();
Above PHP Code is Converted to this JavaScript Code
var FooInt = (function() < function FooInt() < window.__IS_INHERITANCE__ = false; __INTERFACE_NEW__(); >FooInt.prototype.fooIntFunc1 = function(a, b) < __INTERFACE_FUNC__(); >; return FooInt; >)(); var FooAbs = (function() < function FooAbs() < window.__IS_INHERITANCE__ = false; >__extends(FooAbs, null, arguments[1]); FooAbs.prototype.__isAbstract__ = true; FooAbs.prototype.fooAbsFunc1 = function(a, b) < __ABSTRACT_FUNC__(); >; FooAbs.prototype.fooAbsFunc2 = function(a, b) < return a + b + 10; >; return FooAbs; >)(null, [FooInt]); var FooParent = (function(parent) < function FooParent() < var __isInheritance = __IS_INHERITANCE__; window.__IS_INHERITANCE__ = true; parent.call(this); this.foo = 5; if (__isInheritance == false) < this.__construct(); >> __extends(FooParent, parent); FooParent.prototype.__construct = function() < this.foo = 5; >; FooParent.prototype.fooAbsFunc1 = function(a, b) < parent.prototype.fooAbsFunc2.call(this, 1, 5); return a + b; >; FooParent.prototype.fooIntFunc1 = function(a, b) < if (typeof b == 'undefined') b = 5; return a + b + 5; >; FooParent.fooStatic = function() < return 10; >; return FooParent; >)(FooAbs); var FooChild = (function(parent) < function FooChild() < window.__IS_INHERITANCE__ = true; parent.call(this); this.foo = 6; >__extends(FooChild, parent); FooChild.prototype.fooIntFunc1 = function(a, b) < if (typeof b == 'undefined') b = 5; return a + b; >; FooChild.prototype.testParent = function() < assert_(this.fooIntFunc1(5, 5), 10, 'testParent 1'); assert_(parent.prototype.fooIntFunc1.call(this, 5, 5), 15, 'testParent 2'); >; return FooChild; >)(FooParent); var fooParent; fooParent = new FooParent(); var fooChild; fooChild = new FooChild(); assert_(fooParent instanceof FooParent, true, 'fooParent instanceof FooParent'); assert_(fooParent instanceof FooInt, true, 'fooParent instanceof FooInt'); assert_(fooChild instanceof FooChild, true, 'fooChild instanceof FooChild'); assert_(fooChild instanceof FooParent, true, 'fooChild instanceof FooParent'); assert_(fooChild instanceof FooAbs, true, 'fooChild instanceof FooAbs'); assert_(fooChild instanceof FooInt, true, 'fooChild instanceof FooInt'); assert_(FooChild.fooStatic(), 10, 'FooChild::fooStatic()'); fooChild.testParent();
Related posts:
- Wordle Clone JavaScript – Full Source Code
- How to Make Python Checkers Game? [Full Source Code]
- Build an Online Tool to Unminify JS, HTML, CSS, XML and JSON Source Code
- 12 Best PHP Code Beautifier and Formatter Online 2023
- JavaScript Word Guessing Game Source Code
- JavaScript Snakes and Ladders Multiplayer Game Source Code
- PHP 7 Compile SCSS to CSS Code in Command Line Using scssphp Library
- Facebook Login Page HTML and CSS Source Code (Free Download)
- Python Elasticsearch Autocomplete Input Example Source Code
- C++ OpenGL 2D Bubble Shooter Game Source Code
- Python Random Lottery Number Generator Game Source Code
- How to Make Pokemon Encounter Calculator [Get Source Code]
- 21 Places To Find Freelance Web Developer Jobs Online 2023
- JavaScript Code Plays Google Dinosaur/T-Rex Game Automatically
- Webcam Face Detection Using JavaScript, PHP, and MySQL 2023
- PHP 7 FPDF Example: Create PDF Document From Text File Using Javascript
- PHP 7 jsPDF Html2Canvas Example: Send Generated PDF as Email Attachment to Client Using JavaScript
- Python 3 Code to Find Common Words in Two Strings
- Python 3 Code to Find Uncommon Words Between Two Strings in Command Line
- Carbon Language Tutorial with Syntax and Code Examples