Solved

php namespace question with Twilio

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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