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

Comparison of javascript and PHP dynamically adding methods to classes

亚连
Release: 2018-05-26 17:42:47
Original
1227 people have browsed it

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()
Copy after login

b. Output:

hello

2. PHP :

a. Code:

<?php
class a{

}
$a = new a();
$a->say = function(){
echo "hello";
};
$say = $a->say;
$say();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template