FuelPHP 中使用的 OOP
我试图理解 FuelPHP 是如何编写的。由于我不太了解 OOP,所以当这个类时我很困惑: https://github.com/fuel/core/blob/master/classes /date.php
以下是我不明白的方法:
public static function _init()
{
static::$server_gmt_offset = \Config::get('server_gmt_offset', 0);
// some code here
}
public static function factory($timestamp = null, $timezone = null)
{
$timestamp = is_null($timestamp) ? time() + static::$server_gmt_offset : $timestamp;
$timezone = is_null($timezone) ? \Fuel::$timezone : $timezone;
return new static($timestamp, $timezone);
}
protected function __construct($timestamp, $timezone)
{
$this->timestamp = $timestamp;
$this->set_timezone($timezone);
}
首先调用什么? __construct 是做什么的?什么是工厂,它什么时候被使用,它返回什么——它会再次调用自己吗?初始化类后是否调用_init?我真的很困惑,有人能帮我理解吗?谢谢
I'm trying to undestand how FuelPHP was written.. And since I don't know OOP much, I'm puzzled when this class:
https://github.com/fuel/core/blob/master/classes/date.php
Here are methods that I don't understand:
public static function _init()
{
static::$server_gmt_offset = \Config::get('server_gmt_offset', 0);
// some code here
}
public static function factory($timestamp = null, $timezone = null)
{
$timestamp = is_null($timestamp) ? time() + static::$server_gmt_offset : $timestamp;
$timezone = is_null($timezone) ? \Fuel::$timezone : $timezone;
return new static($timestamp, $timezone);
}
protected function __construct($timestamp, $timezone)
{
$this->timestamp = $timestamp;
$this->set_timezone($timezone);
}
What is called first? What __counctruct does? What is factory, when it's used, what it returns - does it call itself again? Is _init called after initializing class? I'm really puzzled, can someone help me understand? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当一个对象被实例化时,第一个被调用的方法是__construct()方法。这称为构造函数,因为它有助于构造类的数据成员并在调用类中的其他方法之前执行任何其他初始化操作。
工厂是一种创建型设计模式,用于根据运行时未知的条件创建类。 - http://en.wikipedia.org/wiki/Factory_method_pattern
_init() 似乎是另一个该库用于设置其类的方法。
为了加深您在这些领域的知识,我建议您阅读 OOP,然后设计模式。
When an object is instantiated, the first method to be called is the __construct() method. This is called a constructor because it helps construct the class's data members and do any other initializing operations before you can call other methods int eh class.
A Factory is a creational design pattern used to create classes based on conditions that would not be known until runtime. - http://en.wikipedia.org/wiki/Factory_method_pattern
_init() seems to be another method that this library uses to set up it's classes.
To further your knowledge in these areas, i suggest you read up on OOP and then design patterns.
这个类看起来像是使用工厂设计模式。在这里查找: PHP - 工厂设计模式
工厂模式允许您在运行时实例化一个类。 _construct 方法在类实例化后立即运行。
This class looks like it is using the factory design pattern. Look it up here: PHP - Factory Design Pattern
The factory pattern allows you to instantiate a class at runtime. The _construct method runs as soon as the class is instantiated.