Jump to content

Class within a class


HaLo2FrEeEk

Recommended Posts

I'm building a PHP class and I need to make use of another class within the one I'm building.  My class needs to have data retrieved from offsite and for that I've always used the Snoopy library (because allow_url_fopen is disabled in my php.ini).  I've tried including and initializing the snoopy class inside my class but it gives me this error:

 

Parse error: syntax error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in [filename hidden by me] on line 42

 

I hid the filename for security reasons.

 

Anyway, is there a way to use an existing class inside of another class?  Any help would be appreciated, and the quicker the better, I'm on a time crunch.

Link to comment
Share on other sites

I'll try to find a section f it I can actually post.  It's unreleased code as of right now so I can't really share it until I'm done with it.  But here:

 

class GetFileshare {

/* Setting some Variables */

        //Path from ROOT to the Snoopy library
$snoopy_dir = $_SERVER['DOCUMENT_ROOT']."/misc/snoopy.php";
        include($snoopy_dir);
        $snoopy = new Snoopy;

/*****************************************************************************\

Function:	getHTML
        Purpose: 	This uses the Snoopy library to retrieve the HTML source
        		from the Fileshare page for the provided gamertag.
        Input:		$gamertag
        Output:		$this->getFilms outputs an array of Films and Filmclips
        		and their titles, fileids, and lengths.

\*****************************************************************************/

function getHTML($gamertag) {
          $snoopy->fetch("http://www.bungie.net/Stats/Halo3/FileShare.aspx?gamertag=".$gamertag);
  $html = $snoopy->results;
          return $html;
          }

 

Line 42 is:

 

$snoopy_dir = $_SERVER['DOCUMENT_ROOT']."/misc/snoopy.php";

 

And yes, my snoopy.php file is in [root]/misc/

Link to comment
Share on other sites

You can't set variables like that or include files like that inside of a class.

 

Include the files you need outside of the class definition, and find another way of handling things other than setting variables like that. Plus, where are your access modifiers? Are you using php4? If so, there should at least be some var keywords thrown in.

Link to comment
Share on other sites

You'll have to bear with me, thisis the first time I've made a php class.  How would I initialize the Snoopy library outside this class and have it able to be called in this class?  This class MUST make use of the snoopy library, it's the only universal way I know of to retrieve a remote url (file_get_contents is disabled on my server and some others, I'm sure.)

 

Setting variables like how?

 

What are access modifiers?

 

Yes, I'm using PHP 4.4.4.

 

What are var keywords?

Link to comment
Share on other sites

You'll have to bear with me, thisis the first time I've made a php class.  How would I initialize the Snoopy library outside this class and have it able to be called in this class?  This class MUST make use of the snoopy library, it's the only universal way I know of to retrieve a remote url (file_get_contents is disabled on my server and some others, I'm sure.)

 

Just move the include outside of the class, think of include like a copy/paste of the content of one file into the file including it (although that isn't exactly correct). You wouldn't want to declare the snoopy class INSIDE of your own class, you want it declared outside of the class in the global scope.

 

What are var keywords?

 

When you define class member variables in php 4 you must prepend them with the var keyword

 

<?php
class MyClass {
    var $variable;
}

 

What are access modifiers?

 

They're only in php 5.

 

Setting variables like how?

 

You'd most likely want to set the variables in the class's constructor.

 

Here's a brief summary on the yucky OOP in PHP 4: http://us.php.net/manual/en/language.oop.php

I'd advise updating to PHP 5 if you want to do any OOP in PHP... PHP 4's OOP isn't very good at all. Besides, PHP 5 has been out for a good 4-5 years now.

Link to comment
Share on other sites

I don't feel like updating to php5 because last time I tried a lot of things on my site stopped working properly and I don't want to have to go fix them.

 

However, if I understand, I can include the class outside the class declaration but still inside the class' php file?  like this:

 

include(somefile);

class someclass {

 

?

Link to comment
Share on other sites

Ok, I changed my code to this:

 

$snoopy_dir = $_SERVER['DOCUMENT_ROOT'].'/misc/snoopy.php';
include($snoopy_dir);
$snoopy = new Snoopy;

class GetFileshare {

/* Setting some Variables */

        //I'll do this later

/*****************************************************************************\

Function:	getHTML
        Purpose: 	This uses the Snoopy library to retrieve the HTML source
        		from the Fileshare page for the provided gamertag.
        Input:		$gamertag
        Output:		$this->getFilms outputs an array of Films and Filmclips
        		and their titles, fileids, and lengths.

\*****************************************************************************/

function getHTML($gamertag) {
          $snoopy->fetch("http://www.bungie.net/Stats/Halo3/FileShare.aspx?gamertag=".$gamertag);
  $html = $snoopy->results;
          return $html;
          }

 

And when I try to run the code using this:

 

include($_SERVER['DOCUMENT_ROOT'].'/misc/getfileshare.php');

$fs = new GetFileshare;
echo $fs->getFilms('HaLo2FrEeEk');

 

getFilms is another function I defined, but it's one of the ones I can't post yet.  Anyway, when I run that code I get this error:

 

Fatal error: Call to a member function on a non-object in /infectionist.com/misc/getfileshare.php on line 59

 

Line 57 is this:

 

$snoopy->fetch("http://www.bungie.net/Stats/Halo3/FileShare.aspx?gamertag=".$gamertag);

 

Do I have to make $snoopy global or do something so that I can use it inside the class?  If I include the file outside the class can I initialize it inside the class?

Link to comment
Share on other sites

There are numerous ways of doing this, the easiest of which is just to pass the instance of the class into the function. You could also pass it to the class in the constructor, build a singleton so you can retrieve the instance without passing it to the class in either the function or the constructor, etc. There's really no way to "tell you how to fix it" because how you will fix it depends upon what this snoopy class is, what its state is, what its function is, etc.

Link to comment
Share on other sites

The snoopy class is designed for people to have an easy way to retrieve offsite data (ie. webpages from another site) using a url, for people who might have allow_url_fopen disabled in their php.ini and so can't use file_get_contents.  The usage of this specific function of snoopy is:

 

include('snoopy.php'); //including the class

$snoopy = new Snoopy; //create an instance of the Snoopy class under the variable $snoopy

$snoopy->fetch(''); //fetch the URL and save it's contents to the Snoopy library variable $results

$html = $snoopy->results; //sets your variable $html to the value of Snoopy's variable $results

 

This lets you retrieve the entire source code for an offsite page, by url, even if allow_url_fopen is disabled in your php.ini, which for me it is.

 

So here's what I've done.  I included snoopy outside the class, I created an instance of snoopy outside the class.  Inside the class, under the function getHTML() I run $snoopy->fetch() on the page I need to fetch, and it doesn't work.

 

Is that enough info?  I can try to explain better.

Link to comment
Share on other sites

There are two things you can do. use the global like you said or instantiate it inside the constructor

 

this is how you do it

 

global example

<?php
$snoopy_dir = $_SERVER['DOCUMENT_ROOT'].'/misc/snoopy.php';
include($snoopy_dir);
$snoopy = new Snoopy;

class GetFileshare {
    var $snoopy;
    function GetFileshare(){
        global $snoopy;
        $this->snoopy=$snoopy;
    }
}
?>

 

in the class only example

<?php
$snoopy_dir = $_SERVER['DOCUMENT_ROOT'].'/misc/snoopy.php';
include($snoopy_dir);

class GetFileshare {
    var $snoopy;
    function GetFileshare(){
      
        $this->snoopy=new Snoopy;
    }
}
?>

Link to comment
Share on other sites

Like I said twice, quite nicely I might add, just pass it into the class at instantiation (like DJ just gave an example of) or pass it into the function when you call it. Global variables are evil, at least encapsulate them in a singleton if you insist on using global state.

 

Example of passing them to a function:

 

<?php
class TestClass {
    function testFunction($snoopy){
        $snoopy->action();
    }
}

$snoopy =& new Snoopy();
$testClass =& new TestClass();
$testClass->testFunction($snoopy);

 

I didn't think I'd need to show you HOW to do it because it's quite simple.

 

If this Snoopy class does indeed maintain state, you may want the class to have its own instance like Dj suggested instead of passing it to the function.

Link to comment
Share on other sites

Quite simple is irrelevant because like I said, I've never written a class before.  It's different than writing functions so I don't know what I was doing.  I got it to work with this code:

 

$snoopy_dir = $_SERVER['DOCUMENT_ROOT'].'/misc/snoopy.php';
require_once($snoopy_dir);
$snoopy = new Snoopy;

class GetFileshare {

        /* Setting some Variables */

        var $snoopy;
var $html = ''; //Where the HTML results from the page fetch goes.
        var $result = array(); //This is where the result of the requests go.

/*****************************************************************************\

        Function:       getHTML
        Purpose:        This uses the Snoopy library to retrieve the HTML source
                        from the Fileshare page for the provided gamertag.
        Input:          $gamertag
        Output:         $this->getFilms outputs an array of Films and Filmclips
                        and their titles, fileids, and lengths.

\*****************************************************************************/

        function getHTML($gamertag) {
          global $snoopy;
          $this->snoopy = $snoopy;
          $snoopy->fetch("http://www.bungie.net/Stats/Halo3/FileShare.aspx?gamertag=".$gamertag);
          $html = $snoopy->results;
          return $html;
          }

 

Yes, I used a global, but the second example DJ Kat gave me didn't work.  It works great now.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.