Home  >  Article  >  Web Front-end  >  Comparison of javascript and PHP dynamically adding methods to classes

Comparison of javascript and PHP dynamically adding methods to classes

亚连
亚连Original
2018-05-26 17:42:471139browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn