简体   繁体   English

js中的模块和php中的类有什么区别?

[英]What is the difference between Modules in js and Classes in php?

I am learning about modules online and it seems like modules in js and classes in php are very similar. 我正在在线学习模块,似乎js中的模块和php中的类非常相似。 Both group functions together for easier to understand coding. 两组功能一起使用,以便于理解代码。 Functions can be declared in both and made public or private. 可以在函数中声明函数,也可以将函数设为公共或私有。 How are they similar in use and how are they different? 它们在用法上有何相似之处,又有何不同?

Javascript's modules provides some nice features like encapsulation, the private state and even inheritance from other modules. Javascript的模块提供了一些不错的功能,例如封装,私有状态,甚至从其他模块继承。 While they provide some of the features of classes, as in PHP, they are not. 尽管它们提供了类的某些功能,但在PHP中却没有。 They try to build on the existing Javascript functonality to emulate classes, hence why the confusion. 他们尝试在现有Javascript功能的基础上模拟类,因此感到困惑。 ie they are built to look and feel like classes . 即它们的外观和感觉就像类

Javascript's modules are instances of an anonymous function assigned to a variable. Javascript模块是分配给变量的匿名函数的实例。 Therefore they have all the features of a function where their code is executed top to bottom, they have and sometimes use a return statement (in PHP classes no statements can be run directly apart from field definition and assignment) and they even have access to global variables . 因此,它们具有函数的所有功能,这些函数的代码从上至下执行,它们有时甚至使用return语句 (在PHP类中,除字段定义和赋值外,任何语句都不能直接运行),甚至可以访问global变量 In PHP, on the other hand a class, or rather it's methods, cannot access a variable that is not in the class itself. 另一方面,在PHP中,类或类的方法不能访问不在类本身中的变量。 In order to access global variables a class method or static function has to explicitly call the variable ie global $a inorder to import it. 为了访问全局变量,类方法或静态函数必须显式调用变量,即global $a以便将其导入。 In js modules, all global vars are accessible but sometimes one chooses to explicity import them for neater code (function(a){})(imported); 在js模块中,所有全局变量都可以访问,但有时人们选择显式导入它们以获取更整洁的代码(function(a){})(imported);

Another important issue is data abstraction. 另一个重要问题是数据抽象。 While js modules provide private states for the fields, PHP's classes, just like C++, java, python etc, provide more security to the properties. js模块为字段提供私有状态,而PHP的类(如C ++,java,python等)为属性提供了更高的安全性。 It allows for base classes using the abstract class and interface keywords whereby class methods and attributes are only defined or structured but not used. 它允许使用abstract classinterface关键字使用基类,从而仅定义或结构化类方法和属性,而不使用它们。

PHP classes also have constructors and destructors, that are called when the class object is initialized and on the last mention used to destroy the object. PHP类还具有构造函数和析构函数,它们在初始化类对象时被调用,并且在最后提及时用于销毁该对象。 Granted, you can create functions in modules to run when you want, in PHP on the other hand, functions in the method are only executed when they are called either by the object, the class or other functions. 当然,您可以在模块中创建要在需要时运行的函数,而在PHP中,方法中的函数仅在对象,类或其他函数调用它们时才执行。

In classes there are static functions, these can be called without even having an object of the class and run independent of objects, on the other hand in js, everything is an object; 在类中有静态函数,即使没有类的对象也可以调用这些静态函数,而这些函数独立于对象运行,另一方面,在js中,一切都是对象。 which defeats the point of static functions. 这打败了静态函数的观点。

They are similar in that: both have inheritance, where you can extend an existing module with a new one, and in PHP you can use extends to inherit from a parent class. 它们的相似之处在于:两者都具有继承关系,您可以在其中扩展现有模块并使用新模块,而在PHP中,您可以使用extends从父类继承。 They both have private data states preventing external access, they both group and package data and methods, and both are awesome when utilized properly. 它们都具有防止外部访问的私有数据状态,它们都对数据和方法进行分组和打包,并且如果使用得当,它们都很棒。

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

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