Answer the question
In order to leave comments, you need to log in
What is the best way to organize the work of several classes under one head?
Hello. I am writing for myself a set of helpers for working with numbers, text, dates, and so on. Each entity (text, numbers) has a set of classes that are responsible for handling a particular situation. I want to combine them nicely.
Just for example:
class math {
public function sum($a, $b){
return (int)$a + (int)$b;
}
public function mod($a, $b) {
return (int)$a % (int)$b;
}
public function div($a, $b) {
return (int)$a / (int)$b;
}
}
class math2 {
public function sumf($a, $b){
return (float)$a + (float)$b;
}
public function modf($a, $b) {
return (float)$a % (float)$b;
}
public function divf($a, $b) {
return (float)$a / (float)$b;
}
}
$math = new Number();
echo $math->sum(1, 2);
echo $math->sumf(1.3, 2.7);
Answer the question
In order to leave comments, you need to log in
Your example is not well thought out.
It would be more logical to assume that there is some common interface that will unite all classes and at the same time provide common functions over operands of various types.
Let's say you need to perform addition with two arguments.
For example like this:
$math = new Calculator(); // в данном случае Number плохое имя, т.к. обозначает число, а не аггрегатор вычислительных операций
echo $math->sum(1, 2); // вернет 3
echo $math->sum(1.3, 2.7); // вернет 4.0
echo Calculator::sum(1, 2); // вернет 3
echo Calculator::sum(1.3, 2.7); // вернет 4.0
class Calculator
{
/**
* Метод предназначен для суммирования двух аргументов
* @param number $firstArgument первый аргумент
* @param number $secondArgument второй аргумент
*/
public static function sum($firstArgument, $secondArgument)
{
return $firstArgument + $secondArgument;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question