Solved

php namespace question with Twilio

Posted on 2016-11-29
4
16 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 108

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 6

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now