使用php通过oop方法将数字转换为数字格式


Using php to convert a number to number format with oop methods

如果我有一个函数,例如将数字转换为数字格式,我显然希望它接受any输入变量并返回对same输入变量的更改。然而,下面不工作,我不知道为什么。我知道如何使用number_format等,但我不知道它如何适合OOP:

Class generic{
public function numeric($num){
    if(!is_numeric($num) && !is_float($num)){
        throw new Exception("Inputed number is not numeric.");
    }
    //Convert the input to a number format
    $this->num = number_format($num,2);  
    echo $this->num; // shows a number in the new format..
    //Return the input with its new value
    $num = $this->num;
    return $num;
    }
  }

 class someother class {
  //function receiving an array and processing the numbers from it.
 public function display_info($data) {
        foreach($data->listing as $item) {
           $price = $price+$item->price;
        } 
             echo $price; //this shows a number in original format.
                    //Process $price through the generic class 'numeric' function
        generic::numeric($price);
        echo '<br/>Total:'.$price.'<br/>'; //This also shows the original format
                    $output = $this->someotherfunction($price);
        return $output;
        }
 public function someotherfunction($data) {
 //Manipulate the data
 return $data;
 }
   }

   //Create a new display object to process the pre-existing array $data
   $test = new someother();
   //Process an array containing 
   $test->display_info($data);

如果你能帮我理解这个,我将非常感激。

$this->num = $num vs $num = $this->num是如何工作的?

class Generic {
    private $num;
    public function __construct($num) {
        $this->num = $num;
    }
    public function numeric() {
        if(!is_numeric($this->num) && !is_float($this->num)){
            throw new Exception("Inputed number is not numeric.");
        }
        $this->num = number_format($this->num,2);  
        return $this;
    }
    public function value() {
        return $this->num;
    }
}
class OtherClass {
    private static function setNum($num) {
        return new Generic($num);
    }
    public function display_info() {
        $price = self::setNum(100);
        echo '<br/>Total:'.$price->numeric()->value().'<br/>';
    }
}


   $test = new OtherClass();
   $test->display_info();

如果你真的想保持面向对象编程,但我不认为数字交互是面向对象的封闭,但是我在这里所做的。

你的Generic类保存一个数字,你可以修改几个方法,在这种情况下,我们只有numeric()方法。

在你的display方法中,你调用这个类,并设置它的价格值,我硬编码100,但它可以是任何你想要的数值。

numeric()方法从它的对象中修改$this->num,并返回它的实例时,你可以调用value()方法,它将返回新的修改值。

  • 一旦你将值设置为类
  • 然后你修改它的方法(numeric())
  • 然后将value()传递给echo

输出为:


总:100.00


我的评论中的例子:

class Generic {
    private $num;
    private $normalized;
    public function __construct($num) {
        $this->num = $num;
        $this->normalized = $num;
    }
    public function numeric() {
        if(!is_numeric($this->num) && !is_float($this->num)){
            throw new Exception("Inputed number is not numeric.");
        }
        $this->num = number_format($this->num,2);  
        return $this;
    }
    public function toInt() {
        $this->num = intval($this->num);
        return $this;
    }
    public function printR() {
        echo "<pre>".print_r($this->num, true)."</pre>";
    }
    /**
     * As normalized was set to the original value of $num and was not modified,
     * you will recieve what you have put from the beginning to the constructor
     */
    public function normalize() {
        $this->num = $this->normalized;
        return $this;
    }
    /**
     * You can use this method to retrieve the original value (without calling normalize())
     * And you will still have the modified $num, if you want to use it
     */
    public function originalValue() {
        return $this->normalized;
    }
    public function value() {
        return $this->num;
    }
}
class OtherClass {
    private static function setNum($num) {
        return new Generic($num);
    }
    public function display_info() {
        $price = self::setNum(100);
        /**
         * numeric() changes it with number_format()
         * <pre>100.00</pre>
         */
        $price->numeric()->printR();
        /**
         * toInt() changes it to integer
         * <pre>100</pre>
         */
        $price->toInt()->printR();

        $price->numeric();
        /**
         * The original value is still kept
         * 100
         */
        echo $price->originalValue();
        /**
         * But the object is changed (because of numeric() call)
         * 100.00
         */
        echo $price->value();

        $price->normalize();
        /**
         * After being normalized to the original value
         * 100
         */
        echo $price->value();

    }
}

您需要了解作用域,以及变量在php中如何传递。泛型类中的函数被传递一个price变量的副本——这个函数不会对其他对象中的原始price变量产生任何影响。

做你想做的事:

Class generic{
    //must be static to be called statically
    public static function numeric($num){
        if(!is_numeric($num) && !is_float($num)){
            throw new Exception("Inputed number is not numeric.");
        }
        return number_format($num,2);
    }
}
class someothere{
    //function receiving an array and processing the numbers from it.
    public function display_info($data) {
        foreach($data->listing as $item) {
            $price = $price+$item->price;
        }
        //the generic::numeric function RETURNS a value, it does NOT modify the original
        echo '<br/>Total:'.generic::numeric($price).'<br/>';
    }
}

如果出于某种原因,您确实希望函数影响原始价格变量,则需要通过引用传递它:

Class generic{
    //must be static to be called statically
    //notice the & symbol that means that a refernce rather than a copy is passed
    public static function numeric(&$num){
        if(!is_numeric($num) && !is_float($num)){
            throw new Exception("Inputed number is not numeric.");
        }
        //edits the original variable, no need to return anything
        $num = number_format($num,2);
    }
}
class someothere{
    //function receiving an array and processing the numbers from it.
    public function display_info($data) {
        foreach($data->listing as $item) {
            $price = $price+$item->price;
        }
        generic::numeric($price);//price is now editied
        echo '<br/>Total:'.$price.'<br/>';
    }
}