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"
is_file_static
) and instance method (asis_file
)? Etc.$this
in static methods on PHP (except with workaround).