【问题标题】:How to implement common method in php?如何在php中实现常用方法?
【发布时间】:2017-06-22 23:02:46
【问题描述】:

如何在一个类中实现一个php中每个用户都可以使用的方法? 我的想法是不要在每个实体中重复 readsales 的实现

我有 3 个用户:

admin readSales 函数, 经理 readSales 功能, 员工、insertSale 和 readSales 函数

是否可以在单个类中实现常用方法?然后将每个方法调用到子类?

 abstract class commonMethods {

 abstract readSales() {

   $pdo = new PDO();
     //statements
     //readSales
     //return $list;
   }

  }



 class Manager extends commonMethods {

  function readSales(){
    return readSales();
  }
}

【问题讨论】:

    标签: function class abstract entities


    【解决方案1】:

    是的,这是可能的,您需要的是特征或抽象类。
    这是一个抽象类的例子:

    <?php
    
    /**
     * The abstract class
     */
    abstract class CommonMethods
    {
    
        public function readSales(){
            // Your code
        }
    
        public function hello(){
            echo "Hello";
        }
    
    }
    
    /**
     * The Class
     */
    class Manager extends CommonMethods
    {
        // No need to add the "readSales" or "hello" method
        // Since we had extended them
    
        public function world(){
            echo "World";
        }
    
    }
    
    $Manager = new Manager;
    $Manager->readSales(); // Works!
    $Manager->hello(); // Output: "Hello"
    $Manager->world(); // Output: "World"
    
    // The final output: "HelloWorld"
    

    参考:http://php.net/manual/en/language.oop5.abstract.php

    【讨论】:

      猜你喜欢
      • 2012-03-27
      • 1970-01-01
      • 2023-03-11
      • 2011-02-06
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多