Solved

php namespace question with Twilio

Posted on 2016-11-29
4
33 Views
Last Modified: 2016-11-29
I want to add twilio class inside my own but I am having issues with the required "use" statement they have. Twilio uses namespaces and this is an area I am not use to using.

when I call the "use" inside my extended class I get a white screen. I know this is not where it goes. When I don't use the namespace twilo does not load. How can I initialize and use the twilo object inside my extended class?

<?php
include PATH."/class/tools.class.php";

//Twilio credentials
define('sid','mysidnumber');
define('token','mytokennumber');
define('number','+mytwiliophonenumber');

class twilio extends tools {

        public function load_twilio() {

                require_once('twilio-php-master/Twilio/autoload.php');
                // Use the REST API Client to make requests to the Twilio REST API
                //use Twilio\Rest\Client; (this does not work here)

                $client = new Client(sid, token);
                print "This will not display because Client is not initializing";
        }
}
?>

Open in new window

0
Comment
Question by:Robert Saylor
  • 2
4 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41905985
Checking here: http://php.net/manual/en/language.namespaces.importing.php
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.
My guess is that your use needs to come right after <?php
0
 
LVL 7

Expert Comment

by:Mukesh Yadav
ID: 41905996
First of all enable error reporting using error_reporting(E_ALL); so that you can know what thing is causing issue;
Then make sure the path you are using to load twilio is correct;

Yes, use can't be the first statement in the file;

There is no need to add use statement after including the autoload.php it loads all the required files;
0
 

Author Closing Comment

by:Robert Saylor
ID: 41906012
that actually worked. I did not think it would so I did not try it.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41906020
Yay!

Also, I agree with the recommendation for error_reporting(E_ALL) -- this can be very helpful!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question