Home >Web Front-end >JS Tutorial >Comparison of javascript and PHP dynamically adding methods to classes
This article mainly introduces the relevant information on the comparison between JAVASCRIPT and PHP for dynamically adding methods to classes. Friends in need can refer to
1. JAVASCRIPT:
a. Code:
var a = function(){ } var my_a = new a() my_a.say = function(){ console.log('hello') } my_a.say()
b. Output:
hello
2. PHP :
a. Code:
<?php class a{ } $a = new a(); $a->say = function(){ echo "hello"; }; $say = $a->say; $say();
b. Output:
hello
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
ajax uses json to implement data transmission
Use Promise to solve the code caused by multiple asynchronous Ajax requests Nesting problem
Ajax passing JSON instance code
The above is the detailed content of Comparison of javascript and PHP dynamically adding methods to classes. For more information, please follow other related articles on the PHP Chinese website!