Solved

php namespace question with Twilio

Posted on 2016-11-29
4
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 110

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 9

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

751 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