English 中文(简体)
OOP used in FuelPHP
原标题:

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

最佳回答

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.

问题回答

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.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签