1

I need the getdata() function from class-GetData.php to call inside this 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();
}
?>

class-GetData.php

<?php

    class GetData
    {
       public function getdata()
       { 
          //something here
       }
    }

    $getData = new GetData();

But i am getting undefined function error:

Call to undefined function getdata()

3
  • 1
    getData() is a method in the GetData class. You need to use $getData->getData(), call the method on the object. Usually, you wouldn't initialize the object in the class-file though Commented Sep 5, 2017 at 9:17
  • $getData->getdata() Commented Sep 5, 2017 at 9:18
  • 1
    See php.net/manual/en/language.types.object.php and php.net/manual/en/language.oop5.basic.php Commented Sep 5, 2017 at 9:18

2 Answers 2

1

Use the object of GetData class to call the function created in the class.

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();
}

class-GetData.php

class GetData
{
   public function getdata()
   { 
      //something here
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Do you perhaps want to explain your answer a bit? What has been changed from the original code, and why did you change that? Always have some text to explain your answer.
1

You are calling Class Method like as normal function calling. Inner Class Method need this keyword to call a method in a Class. If you want to call Public function/method from outside of the class, You have to create an Object.

Try to use -

function shortcode_function(){
  ob_start();
  insert_data();
  $getData = new GetData(); #Create an Object
  $getData->getdata();      #Call method using Object
  return ob_get_clean();
}

Example :

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

Here is the explanation for Private,Public and Protected

3 Comments

Or you can use like as (new GetData())->getdata()
Do you perhaps want to explain your answer a bit? What has been changed from the original code, and why did you change that? Always have some text to explain your answer. Why should anyone "try to use" this?
Ahh Yes! I am updating my answer. Thank you @Qirel

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.