【问题标题】:PHP: Include methods from one class.php to output variables in another?PHP:包含一个class.php中的方法以在另一个中输出变量?
【发布时间】:2016-05-02 04:08:26
【问题描述】:

如果我的术语不正确,我们深表歉意。我想使用一个类中的方法/函数,并使用它们从另一个类文件中输出变量。这是我的主要基地:

Sphere.class.php

   <?php

    class SphereCalculator {


    const PI = 3.14;
    const FOUR_THIRDS = 1.33;

    
        public function __construct($radius){
            $this->setRadius ($radius);
        }
   
        public function setRadius ($radius){
            $this->classRadius = $radius;
        }
    
        public function getRadius(){
            return $this->classRadius;
        }
    
    
        public function getArea () {
        
            return self::PI * ($this->classRadius * $this->classRadius);
        }
    
    }

    $mySphere = new SphereCalculator ($newRadius);

所以使用这个类的函数,我想使用include从第二个php文件中输出半径和面积来拉出方法。但我真的不知道从哪里开始。我查阅了很多教程,但它们都是针对两个类的一个 php 文件。这是我所得到的。

App.class.php

 <?php
    include ("Sphere.class.php");
    
    class AppClass {
    
    
    
        function __construct($radius)
        {

        //something here to call $radius from
        //base class and set it to $newRaduis
        
        }
    
        function callSphereClass ($mySphere)
        {
        //something to call $mySphere
        //something here to allocate $newRadius

        echo "The radius is ".$newRadius."<br>";
        echo "The area is ".$mySphere->getArea ()."<br>";
        }
    
    }


    $newRadius = 113;

    $appClass = new AppClass ();
    $appClass->callSphereClass();

    ?>

【问题讨论】:

  • 注意:您可以使用M_PI 常量或pi() 函数获得更准确的数字
  • 或者您只是在寻找类扩展/派生?
  • 我不太确定,但我认为是推导。所以它从源中提取函数,然后变量的输入和输出在第二个。
  • @Ryan Vincent 哦,这很有趣!对于我的初学者大脑来说,这也是可以理解的。很酷!

标签: php class get set php-include


【解决方案1】:

您好,您可以这样做。

<?php
    include ("Sphere.class.php");

    class AppClass {



        function __construct()
        {
            // YOU DO NOT NEED ANYTHING HERE BECAUSE YOU ARE NOT PASSING
            // ANY VALUES INTO THE CONSTRUCTOR
        }

        function callSphereClass ($mySphere)
        {
            //something to call $mySphere
            // CREATE AN INSTANCE OF SphereCalculator
            $createdObj = new SphereCalculator($mySphere);

            //something here to allocate $newRadius
            // USE THE METHODS THROUGH THE OBJECT $createdObj
            echo "The radius is ".$createdObj->getRadius() ."<br />"; // not $newRadius."<br>";
            echo "The area is ".$createdObj->getArea ()."<br>";
        }

    }


    $newRadius = 113; // this needs to be passed into the callSphereClass()

    $appClass = new AppClass();
    $appClass->callSphereClass($newRadius); // like this

?>

【讨论】:

  • 谢谢!! :D 梦想成真!
【解决方案2】:

如果您想在另一个类中使用某个类的某些方法,则必须扩展该类。使用parent::ClassName::调用anchestor的方法。

您可以而且应该将每个类都写在一个单独的文件中。只需使用require_once 加载另一个文件中需要的类。它适用于在一个地方包含项目最常用的类,或者更好地查看spl_autoload mechanism

由于 PHP 中没有经典的多态性,PHP 5.4 引入了特征。现在您可以声明可以被不同类家族继承的方法的“包”。

<?php
class BaseClass
{
  protected 
    $a
  ;

  public function __construct($a)
  {
    $this->a = $a;
  }

  public function do_something()
  {
    echo $this->a . "<br>\n";
  }  
}

class Derived extends BaseClass
{
  protected 
    $b
  ;

  public function __construct($a, $b)
  {
    parent::__construct($a);
    $this->b = $b;
  }

  public function do_something()
  {
    echo $this->a . ' / ' . $this->b . "<br>\n";
  }
}

class Derived2 extends Derived
{
  public function do_something()
  {
    parent::do_something();
    BaseClass::do_something();
  }  
}

$obj = new Derived2(1,2);
$obj->do_something();
?>

【讨论】:

  • 但同样,这是一个 php 文件而不是两个?
  • 我想我想说的是我想include 基本文件不要扩展它,如果这有意义的话?
  • 类扩展不会跨越包含。要扩展在其他文件中声明的类,您甚至必须包含它。正是 OOP 中的封装概念,只有属于类(或其祖先)的方法才能被调用。
  • 第二课Derived2能解释一下吗?我只是不确定如何填写。
【解决方案3】:

在我看来你只是想使用(即实例化)你的类;这应该发生在另一个类中相对不重要。

include 'Sphere.class.php';

class AppClass {

    protected $radius;

    function __construct($radius) {
        $this->radius = $radius;
    }

    function callSphereClass() {
        $sphere = new SphereCalculator($this->radius);
        echo "The area is ", $sphere->getArea();
    }
}


$newRadius = 113;
$appClass = new AppClass($newRadius);
$appClass->callSphereClass();

【讨论】:

    猜你喜欢
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多