如何在其他方法中调用Class方法

时间:2014-01-27 13:03:24

标签: php oop methods

我试着在其他函数中调用我的公共函数。首先想我是面向对象的新手,所以也许我的逻辑不正确但我正在尝试。下来我正在展示我做了什么。我想要什么。< / p>

的config.php

require('core.php');
$url_site = "http://www.mysite.com/musicjee/";

core.php中

 class Core_musicjee

 {   

      public function link_main($url_site)

   {

          $http =$url_site; 
          echo $http;

   }

      public function insert_data()
      {


      echo "Main URL is" // there i want to call link_main($url_site) function ;

      }


}
 $core = new Core_musicjee;

2 个答案:

答案 0 :(得分:2)

您可以在另一个函数中调用类函数,如

public function insert_data()
{
   $this->link_main("YOUR URL");
}

您必须使用$this->来调用另一个Class函数中的任何函数。

答案 1 :(得分:1)

您可以使用PHP的$this伪变量。

来自PHP文档:

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

core.php中

    <?php
class Core_musicjee

 {   

      public function link_main($url_site)

   {

          $http =$url_site; 
          echo $http;

   }

      public function insert_data($url_site)
      {


      echo "Main URL is "; // there i want to call link_main($url_site) function ;
          $this->link_main($url_site);

      }


}

?>

的config.php

   <?php
      require('core.php');
      $url_site = "http://www.mysite.com/musicjee/";

     $core = new Core_musicjee();
     $core->insert_data($url_site);
   ?>