PHP Class new item, cannot call it correctly?

Hi Experts,

I am banging my head against the wall, and I am sure it is due to not knowing PHP as well as I should!
I am trying to call this new class, and keep getting the error "Undefined variable: Google_Playlist" which I can only assume is due to me not calling the class properly.

$youtube = new Google_YoutubeService($client);
//update Playlist Name here
			$ListOb = new $youtube->$Google_Playlist();
			$InSnip = new $youtube->Google_PlaylistSnippet();
			$InSnip.setTitle(str_replace("Candidates Federal","Candidates Australian Federal",$title));

Open in new window

I am using this PHP library to access Youtube API, with some success, but have gotten stuck at needing to create a Google_Playlist class item to do an update.

Google API Library

Your help and thoughts are appreciated greatly.
Craig LambieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
My guess would be that you need to ditch the new in this line:

$ListOb = new $youtube->$Google_Playlist();

Open in new window

Craig LambieAuthor Commented:
sorry, no that doesn't work :(

I really need someone that is a PHP expert to take a look at the library and explain to me the structure a little, enough to get the new class object initiated, then I think I should be able to manage
Where have you initialized this variable $Google_Playlist
I hope for a function Google_Playlist() you have added $ symbol and made it to an variable.
$ListOb = $youtube->Google_Playlist();

use this way.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Julian HansenCommented:
Ok quite a bit wrong with this

$ListOb = new $youtube->$Google_Playlist();
$InSnip = new $youtube->Google_PlaylistSnippet();

You are trying to instantiate a variable you need to instantiate the class like so

$ListOb = new Google_Playlist();
$InSnip = new Google_PlaylistSnippet();

Next '.' delimiters are not recognised in PHP you should be using ->

Not sure if this does what you want but the correct PHP method for writing your code is

$youtube = new Google_YoutubeService($client);
$ListOb = new Google_Playlist();
$InSnip = new Google_PlaylistSnippet()
$InSnip->setTitle(str_replace("Candidates Federal","Candidates Australian Federal",$title));

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Craig LambieAuthor Commented:
Thanks for helping with the syntax, that have me a new error :(

Can you see what this property should be? I can't see any "required" properties.

Notice: Undefined property: Google_YouTubeService::$Google_Playlist in xxx on line 70 (that is line 2 in your example above julianH)

Julian HansenCommented:
that is line 2 in your example above julianH)
Doesn't make sense if it is

What the error is saying is that you have tried to do something like


Which won't work because as the error says Google_Playlist is not a member of Google_YouTube. It is a separate class

What does line 70 say exactly - can you post a full source listing?
Craig LambieAuthor Commented:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
require_once '../StdPHPErrors.php';

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.

$client = new Google_Client();
$model = new Google_Model();

//xxxxxVideo Uploader ID stuff
//$client->setDeveloperKey('xxxxx'); // Server Key
$client->setDeveloperKey('xxxxx'); //Browser Key

// ID stuff

$youtube = new Google_YoutubeService($client);

if (isset($_GET['code'])) {
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

if (isset($_SESSION['token'])) {
$htmlBody = '';
if ($client->getAccessToken()) {
  try {
	$playlists_optParams = array('mine' => 'true','maxResults' => '50');
    $playlistItems = $youtube->playlists->listPlaylists('snippet',$playlists_optParams);

    $htmlBody .= '<table>';
	$htmlBody .= '<tr><th>Title</th><th>Change Req</th><th>Changed?</th></tr>';
    foreach ($playlistItems['items'] as $item) {	
		//$htmlBody .= "<li>".$item['snippet']['title']." ID:".$item['id']."</li>";
		$htmlBody .= '<tr>';
		$title = $item['snippet']['title'];
		$id = $item['id'];
		$htmlBody .= '<td>'.$title.'</td>';
		if(!strstr($title,"Australian") && strstr($title, "Senate")) {
			$htmlBody .= '<td>Yes</td>';
			//update Playlist Name here
			$ListOb = new $youtube->Google_Playlist();
			$InSnip = new $youtube->Google_PlaylistSnippet();
			$InSnip.setTitle(str_replace("Candidates Federal","Candidates Australian Federal",$title));
			$playlistUpdate = $youtube->playlists->update('snippet',$ListOb);
				//error with update
				$htmlBody .= '<td class="bad">No</td>';
				//successful update
				$htmlBody .= '<td class="good">Yes</td>';
		} else {
			$htmlBody .= '<td>No</td>';
		$htmlBody .= '</tr>';
    } //end for each item in Playlist
	$htmlBody .= '</table>';
  } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',

  $_SESSION['token'] = $client->getAccessToken();
} else {
	$state = mt_rand();
	$_SESSION['state'] = $state;

	$authUrl = $client->createAuthUrl();
	$htmlBody = <<<END
	<h3>Authorization Required</h3>
	<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>



<!doctype html>
    <title>My Uploads</title>
		table tr td { border: 1pt black solid; }
		.bad {color:red;}
		.good {color:green;}
	<h1>Youtube Project Output:</h1>
    <?php print $htmlBody; 
		echo1("<br /> Complete:".date("H:i:s"));	

Open in new window

Julian HansenCommented:
Line 70: is
$ListOb = new $youtube->Google_Playlist();

Open in new window

Which is not the same as Line 2 of my post
$ListOb = new Google_Playlist();

Open in new window

Note the absence of the $youtube-> prefix in the second line of code.

Line 70 is trying to instantiate an object (new) using an existing object method call - which is like saying "Please pass the confused"

Line 12 - you have a missing closing apostorphe - although this is probably a result of you removing the name and replacing with xxxx

Other than that it does not appear that you have made any of the recommended changes (Lines 70 to 75).
Craig LambieAuthor Commented:
Sorry, I really should look more carefully, and get some rest! Thanks for the help, very useful!
Julian HansenCommented:
You are welcome - thanks for the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.