[英]Calling function from another file
我需要getdata()
從函數class-GetData.php
到這里面調用shortcode_function()
require_once plugin_dir_path( __FILE__ ) . '/class-GetData.php';
add_shortcode('registration-plugin','shortcode_function');
function shortcode_function()
{
ob_start();
insert_data();
getdata(); //from GetData.php
return ob_get_clean();
}
?>
類訪問getdata.php
<?php
class GetData
{
public function getdata()
{
//something here
}
}
$getData = new GetData();
但是我收到未定義的函數錯誤:
調用未定義的函數getdata()
使用GetData類的對象可以調用在該類中創建的函數。
require_once plugin_dir_path( __FILE__ ) . '/class-GetData.php';
add_shortcode('registration-plugin','shortcode_function');
function shortcode_function()
{
ob_start();
insert_data();
$getData = new GetData(); //Create Getdata object
$getData->getdata(); //call function using the object
return ob_get_clean();
}
類訪問getdata.php
class GetData
{
public function getdata()
{
//something here
}
}
您正在像正常的函數調用一樣調用Class Method
。 內部Class Method
需要this
關鍵字來調用類中的方法。 如果要從類外部調用Public
函數/方法,則必須創建一個Object
。
嘗試使用-
function shortcode_function(){
ob_start();
insert_data();
$getData = new GetData(); #Create an Object
$getData->getdata(); #Call method using Object
return ob_get_clean();
}
范例:
class GetData{
public function getdata() {
//something here
}
public function TestMethod(){
$this->getdata(); #Calling Function From Inner Class
}
}
$getData = new GetData(); #Creating Object
$getData->getdata(); #Calling Public Function from Outer Class
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.