Magento getSingleton混乱 [英] Magento getSingleton confusion

查看:90
本文介绍了Magento getSingleton混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对接到Mage::getSingleton的电话感到有些困惑,希望有人能帮助我更好地理解.

I'm a little confused about calls I see to Mage::getSingleton, and I'm hoping someone can help me understand a little better.

我已经看到了执行此操作的一段核心代码:

I have seen a piece of core code that does this:

Mage::getSingleton('customer/session')->isLoggedIn()

我不了解PHP,但是我认为我可以根据 getSingleton 方法名称做出一个安全的假设,即只会指定一个类的实例(该类被指定为一组)类名称,并解析为app/code/core/Mage/Customer/Model/Session.php-包含类Mage_Customer_Model_Session.

I don't know PHP, but I think I can make a safe assumption from the getSingleton method name that there will be only one instance of the class specified (the class being specified as a grouped class name, and resolving to app/code/core/Mage/Customer/Model/Session.php - containing class Mage_Customer_Model_Session.

问题1 -

getSingleton方法如何知道在类的模型"文件夹中查找?

How did the getSingleton method know to look in the Model folder for the class?

问题2 -

因此,整个类都有一个实例……我想说JVM,就像我来自Java一样,但是我将说PHP引擎,希望这是模糊的正确术语. Mage_Customer_Model_Session没有传入客户ID或任何此类标识符,但是我们称为方法isLoggedIn()!假设每个客户没有一个Mage_Customer_Model_Session实例,当我们不告诉客户我们在说什么客户时,我们如何问一个单例是否登录了客户?

So there is one instance of the class for the whole ... I want to say JVM as I am from a Java background, but I'll say PHP engine in the hope that that is vaguely the correct terminology; the Mage_Customer_Model_Session is not passed in a customer id or any such identifier, yet we call the method isLoggedIn()! Give there is not a Mage_Customer_Model_Session instance per customer, how can we ask a singleton if a customer is logged in when we do not tell it what customer we are talking about?

问题3 -

我已经看到了对Mage::getSingleton('core/session')Mage::getSingleton('customer/session')的呼叫-有什么区别?

I've seen calls to Mage::getSingleton('core/session') and to Mage::getSingleton('customer/session') - what is the difference?

谢谢您的帮助.

推荐答案

首先,在接触Magento之前,必须了解PHP与Java具有根本不同的过程模型,这一点很重要. PHP单例(无论Magento是否参与)都是每个HTTP请求的类的单个实例. PHP程序在内存中的持久性不同于Java程序,因此请相应地调整对单个"的期望.

First, before we get to Magento, it's important to understand that PHP has a radically different process model than Java.  A PHP singleton (regardless of Magento's involvement) is a single instance of a class per HTTP Request.  A PHP program isn't persistent in memory the same way a Java program is, so adjust your expectations of a "singleton" accordingly.   

接下来,重要的是要了解Magento是使用PHP在顶部上构建的PHP框架,在许多情况下,原始的Magento开发人员都希望将其推向更像Java的体系结构.因此,您将看到的东西看起来很熟悉,很熟悉,但可能在某些方面与您以前使用的方式有所不同,因为它们仍然需要使用PHP的Universe版本.

Next, it's important to understand that Magento is a framework built on top of PHP, using PHP, and in many cases the original Magento developers wanted to push things towards a more Java like architecture.  So, you're going to see things that look familiar, are familiar, but likely differ in some major way from what you're used to because they still need to hew to PHP's version of the universe. 

Magento使用工厂模式来实例化Helper,Block和"Model"类.字符串

Magento uses a factory pattern to instantiate Helpers, Blocks, and "Model" classes.  The string

core/session

是类别名.该别名用于在Magento的配置中查找类名.简而言之,该字符串将转换为路径表达式,该路径表达式根据调用的上下文(帮助器,块,模型)搜索Magento的配置文件以派生类名.有关更长的版本,请参见我的Magento的类实例化自动加载文章.

is a class alias.  This alias is used to lookup a class name in Magento's configuration. In short, this string is converted into path expressions that search Magento's configuration files to derive a classname, based on the context (helper, block, model) it was called in. For a longer version, see my Magento's Class Instantiation Autoload article.

在Magento中,模型"的概念有些模糊.在某些情况下,模型用作域模型或服务模型.在其他情况下,它们被用作更传统的中间件数据库持久性模型.在使用该系统几年之后,我认为考虑模型的最安全方法是它们是Magento试图取消直接类实例化的尝试.

The concept of a "Model" is a little fuzzy in Magento.  In some cases models are used as domain, or service models.  In other cases they're used as a more traditional middleware database persistence models.  After working with the system for a few years, I think the safest way to think about Models is they're Magento's attempt to do away with direct class instantiation.

有两种方法可以实例化模型类.

There's two ways to instantiate a model class. 

Mage::getModel('groupname/classname');
Mage::getSingleton('groupname/classname');

第一种形式将为您提供一个新的类实例.第二种形式将为您提供单例类实例.这种特殊的Magento抽象性允许您从任何 Magento模型类中创建单例,但前提是您坚持使用Magento的实例化方法.也就是说,如果您拨打电话

The first form will get you a new class instance.  The second form will get you a singleton class instance.  This particular Magento abstraction allows you to create a singleton out of any Magento model class, but only if you stick to Magento's instantiation methods.  That is, if you call 

Mage::getSingleton('groupname/classname');

然后随后调用

Mage::getSingleton('groupname/classname');

将返回该单例实例. (这是通过注册表模式实现的).但是,没有什么可以阻止您直接使用任一实例来实例化该类的新实例.

will return that singleton instance.  (This is implemented with a registry pattern). However, there's nothing stopping you from directly instantiating a new instance of the class with either

$o = Mage::getModel('groupname/classname');
$o = new Mage_Groupname_Model_Classname();

这带我们去参加了会议. PHP的请求模型(如HTTP)最初被设计为无状态的.每个请求仅与来自用户的信息一起进入系统.随着语言(和网络)逐渐成为一种应用程序平台,引入了一种允许信息得以持久保存的系统,以替代不断涌现的本地系统.该系统称为会话. PHP会话通过向最终用户程序员公开一个超级全局$ _SESSION数组来工作,该数组允许在每个Web用户的基础上存储信息.通过在用户端将唯一ID设置为cookie,然后使用该cookie作为查找键(也是Web应用程序的标准做法)来实现会话.

Which brings us to sessions.  PHP's request model, like HTTP, was originally designed to be stateless.  Each request comes into the system with, and only with, information from the user.  As the language (and the web) moved towards being an application platform, a system that allowed information to be persisted was introduced to replace the homegrown systems that were cropping up.  This system was called sessions.  PHP sessions work by exposing a super global $_SESSION array to the end-user-programmer that allow information to be stored on a per web-user basis.  Sessions are implemented by setting a unique ID as a cookie on the user end, and then using that cookie as a lookup key (also standard practice for web applications)

依次,Magento系统在PHP的会话抽象之上构建了一个抽象.在Magento中,您可以创建一个继承自基本会话类的会话模型",在其上设置数据成员,然后像使用数据库持久性模型一样保存/加载这些数据成员.区别在于信息存储在会话中而不是数据库存储中.当你看到

In turn, the Magento system builds an abstraction on top of PHP's session abstraction.  In Magento, you can create a "session model" that inherits from a base session class, set data members on it, and save/load those data members just as you would with a database persistence model.  The difference is information is stored in the session instead of the database store. When you see

core/session
customer/session

这是两个不同的会话模型,每个模型都存储不同的数据.一个属于Mage_Core模块,另一个属于Mage_Customer模型.该系统允许模块安全地设置和操作自己的会话数据,而不会意外踩到另一个模块的脚趾,并提供用于处理该数据的逻辑类方法.

these are two different session models, with each one storing different data. One belongs to the Mage_Core module, the other belongs to the Mage_Customer model.  This systems allows modules to safely set and manipulate their own session data, without accidentally stepping on another module's toes, and provide logical class methods for manipulating that data.

希望能回答您提出的问题以及您没有回答的问题.

Hopefully that answers the questions you asked, as well as the ones you didn't.

这篇关于Magento getSingleton混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆