Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php namespace question with Twilio

Posted on 2016-11-29
4
Medium Priority
?
54 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

688 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