Static and not-static: programmer quantum theory

Let me illustrate using the PHP language. The discussion here is, how should I do exactly to solve this problem in a clear and unambiguous mode.

Imagine that I have a class called Path. This class is responsible for defining an object that refers to a path of a file, for example. This class helps me with some tasks, for example:

$pathInstance = new Path("path/to/file.bin");
$pathInstance->exists();             // Return if file exists.
$pathInstance->get_dirname();        // Return the dirname of file.
$pathInstance->get_basename();       // Return the basename of file.
$pathInstance->is_file();            // So on...

However, there is a problem in this methodology when I need to, at the same time, use a static method that allows to perform a task without creating an instance. For instance:

When I call is_file() it’ll check if the internal path if $this is refer to a file. But I want to do it too statically, for instance, Path::is_file("path/to/file.bin").

In PHP this is not recommended, and is required to make a basic workaround. And I personally think is not the right concept to be applied.


Imagine that I have it:

class Path {
    /** @var string */
    private $path;

    /** @param string $path */
    function __construct($path) {
        $this->path = $path;
    }

    /**
     * Normalize the path.
     * @param  boolean $force_normalize Force even if path doesn't exists.
     * @return string|false
     */
    public function normalize($force_normalize = null) {
        if($force_normalize === true) { /* ... */ }
        return realpath($this->path);
    }
}

So I use as:

$pathInstance = new Path("/abc/../def");
echo $pathInstance->normalize(); // output "/def"

But I would like to work TOO statically, for instance:

echo Path::normalize("/abc/../def"); // output "/def"

7

To me, this is a question of class design.

If we are designing for target implementation languages such as C++, Java, and the like (if you will allow them to be lumped together), the questions are

  • what use cases do you want to support?

  • how should their interfaces be structured?

  • how can those two masters be best supported and balanced?

You have given a requirement for the first question – you want static (class) methods and dynamic (instance) methods. For a given method, for now lets call these two methods classMethod and instanceMethod. We’ll give them better names later.

For the second question, the interfaces to the two methods must be different. At present they have different names, but we would like them to be the same if they do the same job. What are the options?

  • We could put them in different classes, but that would just make our design more complex.

  • We could give them the same name, and then they’d need different parameter lists.

The second one is ideal for this problem. We can call our methods myMethod() for the instance method version, and myMethod(path) for the class version.

Regarding implementation, we could make our instance method call the class method, passing it’s path. Or make the class method construct an instance and then call the instance method. Since I can see reasonable implementation alternatives, my high level class design job is done.

So as an implementer, my concerns will be

  • which approach is more natural in my target language?

  • do I want my implementation to match other implementations in other languages?

  • will there be obvious performance losses in choosing one implementation over another? (I say losses here, because while I wish to avoid premature optimization, I also want to avoid “deliberately inefficient” implementations).

Finally, given intuition that one implementation will not have a great advantage over the other, I would implement myMethod() to call myMethod(path).

Hope that helps.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật