PHP: Calling a function inside a class?

How do I call a function inside a class from outside that class (but not inside any class)?

Please supply a simple class and methods/functions showing how you would like to call the function.

The idea is to pinpoint the problem and eliminate ambiguity.

FYI: A function within a class is called a Method.

<?php
class MyClass {
    function myMethod(){
        return 'My Method';

    }
}
$demo =  new MyClass();
echo $demo->myMethod();

I have the code below. I need to acces the function send_to_flow($params = null,$to_phone = null,$input_phone = null)

send_to_flow is there to package up the inputs and send them to Twilio so it can do its thing.

I’m trying to hook a Wordpress form plugin to send some data from a form via that send_to_flow function.

Note, I have some literals in there for testing purposes.

add_action('jet-form-builder/custom-action/cg_registration','package_form_results_flow');

function package_form_results_flow($result){
    $to_number = "+1" . $result['phone_number'];
    $params = array (
        'numberType' => 'MOBILE',
        'firstName' => 'JOHN',
    );
    $number_type = $result['number_type'];
    send_to_flow($params,$to_number);
}
class LCPFlowConnector
{
    public $pluginName = "lcpflowconnector";

    public function displayLCPFlowConnectorSettingsPage()
    {
        include_once "lcpflowconnector-admin-settings-page.php";
    }

    public function addLCPFlowConnectorAdminOption()
    {
        add_options_page(
            "LCP FLOW CONNECTOR PAGE",
            "Flow Connector",
            "manage_options",
            $this->pluginName,
            [$this, "displayLCPFlowConnectorSettingsPage"]
        );
    }

    /**
     * Sanitises all input fields.
     *
     */
    public function pluginOptionsValidate($input)
    {
        $newinput["api_sid"] = trim($input["api_sid"]);
        $newinput["api_auth_token"] = trim($input["api_auth_token"]);
        $newinput["api_flow_sid"] = trim($input["api_flow_sid"]);
        $newinput["api_default_from"] = trim($input["api_default_from"]);
        return $newinput;
    }

    /**
     * Registers and Defines the necessary fields we need.
     *  @since    1.0.0
     */
    public function lcpflowconnectorAdminSettingsSave()
    {
        register_setting(
            $this->pluginName,
            $this->pluginName,
            [$this, "pluginOptionsValidate"]
        );
        add_settings_section(
            "lcpflowconnector_main",
            "Main Settings",
            [$this, "lcpflowconnectorSectionText"],
            "lcpflowconnector-settings-page"
        );
        add_settings_field(
            "api_sid",
            "API SID",
            [$this, "lcpflowconnectorSettingSid"],
            "lcpflowconnector-settings-page",
            "lcpflowconnector_main"
        );
        add_settings_field(
            "api_auth_token",
            "API AUTH TOKEN",
            [$this, "lcpflowconnectorSettingToken"],
            "lcpflowconnector-settings-page",
            "lcpflowconnector_main"
        );
        add_settings_field(
            "flow_sid",
            "FLOW SID",
            [$this, "lcpflowconnectorSettingFlowSid"],
            "lcpflowconnector-settings-page",
            "lcpflowconnector_main"
        );
        add_settings_field(
            "api_default_from",
            "Default From Phone Number",
            [$this, "lcpflowconnectorSettingDefaultFrom"],
            "lcpflowconnector-settings-page",
            "lcpflowconnector_main"
        );
    }

    /**
     * Displays the settings sub header
     *  @since    1.0.0
     */
    public function lcpflowconnectorSectionText()
    {
        echo '<h3 style="text-decoration: underline;">Edit api details</h3>';
    }

    /**
     * Renders the sid input field
     *  @since    1.0.0
     */
    public function lcpflowconnectorSettingSid()
    {
        $options = get_option($this->pluginName);
        echo "
            <input
                id='$this->pluginName[api_sid]'
                name='$this->pluginName[api_sid]'
                size='40'
                type='text'
                value='{$options['api_sid']}'
                placeholder='Enter your API SID here'
            />
        ";
    }

    /**
     * Renders the auth_token input field
     *
     */
    public function lcpflowconnectorSettingToken()
    {
        $options = get_option($this->pluginName);
        echo "
            <input
                id='$this->pluginName[api_auth_token]'
                name='$this->pluginName[api_auth_token]'
                size='40'
                type='text'
                value='{$options['api_auth_token']}'
                placeholder='Enter your API AUTH TOKEN here'
            />
        ";
    }
    /**
     * Renders the flow input field
     *
     */
    public function lcpflowconnectorSettingFlowSid()
    {
        $options = get_option($this->pluginName);
        echo "
            <input
                id='$this->pluginName[api_flow_sid]'
                name='$this->pluginName[api_flow_sid]'
                size='40'
                type='text'
                value='{$options['api_flow_sid']}'
                placeholder='Enter your FLOW here (eg FWXXXXXXXXXXXX)'
            />
        ";
    }
    /**
     * Renders the default from number field
     *
     */
    public function lcpflowconnectorSettingDefaultFrom()
    {
        $options = get_option($this->pluginName);
        echo "
            <input
                id='$this->pluginName[api_default_from]'
                name='$this->pluginName[api_default_from]'
                size='40'
                type='text'
                value='{$options['api_default_from']}'
                placeholder='Enter your default FROM number (eg. +15125555555)'
            />
        ";
    }

/* SEND TO FLOW */
 /* $to number is required, to be in E11 format eg +15555551234
 /* $flow will default to whatever is input on the settings page, but can be overridden
 /* $parameters(array) will send extra parameters with the flow, if necessary
 */ 

    public function send_to_flow($params = null,$to_phone = null,$input_phone = null)
    {


        //gets our api details from the database.
        $api_details = get_option($this->pluginName);
        if (is_array($api_details) and count($api_details) != 0) {
            $TWILIO_SID = $api_details["api_sid"];
            $TWILIO_TOKEN = $api_details["api_auth_token"];
            $TWILIO_FLOW = $api_details["api_flow_sid"];
            $TWILIO_DEFAULT_FROM = $api_details["api_default_from"];
           
        }
        if(is_null($input_phone)){
            $from_phone = $TWILIO_DEFAULT_FROM;
        }
        $ch = curl_init();
        curl_setopt(CURLOPT_POST, TRUE); //post
        $url = "https://studio.twilio.com/v1/Flows/" . $TWILIO_FLOW . "/Executions"; 
       $FROM = $from_phone;
       $paramsd = array(
               'flowFunc' => "CG_NEW_USER_VERIFY",
               'numberType' => "MOBILE",
           );
        curl_setopt($ch, CURLOPT_URL, $url); //the url
        curl_setopt($ch, CURLOPT_USERPWD, "$TWILIO_SID:$TWILIO_TOKEN"); //credentials
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //allow authentication
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'To' => $to_phone,
            'From' => $FROM,
            'Parameters' => json_encode($params)
            )
        );
        curl_exec($ch); //send
    }


}```

The call to the method has to reference an object which is an instance of the class.

$object->send_to_flow($params,$to_number);

Though really the method should have some kind of return, if at least to signal if the operation worked or not.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.