Link to home
Start Free TrialLog in
Avatar of pacidev
pacidev

asked on

using PHP_SELF in a class

I'm trying to use PHP_SELF in a class but it isn't working. I have two files:

classtest.php
page.inc

In classtest.php I create a new web page using the Page class which is defined in page.inc. In page.inc I have a function SetFilename() that gets the basename of the file (I found this function here on ee). Previously, I was using this function directly in a php script file that created a web page (without the use of a class) and it worked. It isn't working using a class.

The value I'm trying to get for the $this->filename variable is:  classtest

I anticipated a problem with this but thought that I just might get the wrong value for $this->filename(page from page.inc). But I'm not getting anything.

classtest.php
--------------
<?php
require('page.inc');
$classtestpage = new Page();
$classtestpage -> Display();
?>

page.inc
---------
<?php
class Page
{
      //class Page's attributes

      var $content;
      var $title = 'Test Title';
      var $keywords = 'Test Keyword, Test Keyword 2';
      var $filename;

      //class Page's operations

      function SetFilename()
      {
            $brk_filename = explode(".", basename($PHP_SELF));
            $num_part = count($brk_filename);
            if( $num_part >1)
                  unset($brk_filename[$num_part-1]);
            $this->filename = implode(".", $brk_filename);
      }

      function Display()
      {
            echo "<html>\n<head>\n";
            $this->DisplayTitle();
            $this->DisplayKeywords();
            echo "</head>\n<body>\n";
            $this->DisplayFilename();
            echo "this is a test from the display function";
            echo "</body>\n</html>\n";
      }

      function DisplayTitle()
      {
            echo '<title>'.$this->title.'</title>';
      }

      function DisplayKeywords()
      {
            echo "<META name =\"keywords\" content=\"".$this->keywords."\">";
      }

      function DisplayFilename()
      {
            echo $this->filename;
      }
}
?>
Avatar of peyox
peyox
Flag of Poland image

Since "register globals" setting is set to OFF by default in newer verisions of PHP, you should use:

$_SERVER['PHP_SELF']
Avatar of pacidev
pacidev

ASKER

I tried that. It didn't work.
Why not use this:

 function SetFilename()
     {
          $this->filename = basename($PHP_SELF,".php"); // returns "classtest";
     }
Avatar of pacidev

ASKER

>Why not use this:
>
>function SetFilename()
>     {
 >         $this->filename = basename($PHP_SELF,".php"); // returns "classtest";
 >    }

The function I'm using will work for any filename, with or without an extension, and it works when I'm not using a class. The function you provided also does not work in the class.
Avatar of Roonaan
You have to use the __FILE__ constant.

$this->filename = basename(__FILE__);

__FILE__ always holds the filename of the file the constant is used:

index.php:

echo __FILE__ ;//=> wwwhome/index.php
include 'test.php';

test.php:
echo __FILE__; => wwwhome/test.php

-r-
Avatar of pacidev

ASKER

>You have to use the __FILE__ constant

This also didn't work in the class.

I've figured out the problem -- I wasn't calling the SetFilename() function anywhere. The following is the solution that works. Also, see at the bottom an alternative SetFilname() function.

classtest.php
--------------
<?php
require('page.inc');
$classtestpage = new Page();
$classtestpage -> Display();
?>

page.inc
---------
<?php
class Page
{
      //class Page's attributes

      var $content;
      var $title = 'Test Title';
      var $keywords = 'Test Keyword, Test Keyword 2';
      var $filename;


      //class Page's operations

      function Page()
      {
            $this->SetFilename();
            $this->SetTitle();
            $this->setKeywords();
      }

      function SetFilename()
      {
            global $PHP_SELF;
            $brk_filename = explode(".", basename($PHP_SELF));
            $num_part = count($brk_filename);
            if( $num_part >1)
                  unset($brk_filename[$num_part-1]);
            $this->filename = implode(".", $brk_filename);
      }

      function SetTitle()
      {
            $this->title = 'Title from SetTitle function';
      }

      function SetKeywords()
      {
            $this->keywords = 'Keywords from SetKeywords function';
      }

      function Display()
      {
            echo "<html>\n<head>\n";
            $this->DisplayTitle();
            $this->DisplayKeywords();
            echo "</head>\n<body>\n";
            $this->DisplayFilename();
            echo "<br>";
            echo "this is a test from the display function";
            echo "</body>\n</html>\n";
      }

      function DisplayTitle()
      {
            echo '<title>'.$this->title.'</title>';
      }

      function DisplayKeywords()
      {
            echo "<META name =\"keywords\" content=\"".$this->keywords."\">";
      }

      function DisplayFilename()
      {
            echo $this->filename;
      }
}
?>

Alternative SetFilename() function
---------------------------------------
      function SetFilename()
      {
            $brk_filename = explode(".", basename($_SERVER['PHP_SELF']));
            $num_part = count($brk_filename);
            if( $num_part >1)
                  unset($brk_filename[$num_part-1]);
            $this->filename = implode(".", $brk_filename);
      }

If you use PHP_SELF you have to define it as global. Instead, you can use $_SERVER['PHP_SELF']. I had already tried using $_SERVER['PHP_SELF'] before posting this question, but it didn't work because I wasn't calling the SetFilename function. I'm new to PHP but from what I've read it seems that it would be better to use $_SERVER['PHP_SELF'].

That was the good one :)
Besides try to avoide register globals set to on.
Here's why:
http://us4.php.net/register_globals
Avatar of pacidev

ASKER

>Besides try to avoide register globals set to on.

Do you mean:
Avoid going into php.ini and changing register_globals to 'on'? (I understand why this isn't good)

Or do you mean:
Avoid doing this in php scripts?

In other words, should I avoid declaring variables as 'global' in my scripts? I'm very new to PHP and don't have a handle on all the nuances yet.

Also, thanks for the link (http://us4.php.net/register_globals). Very useful with regard to security.
ASKER CERTIFIED SOLUTION
Avatar of peyox
peyox
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added to this question in more than 21 days, so it is now classified as abandoned..
I will leave the following recommendation for this question in the Cleanup topic area:
Split: peyox {http:#13697957} & lokip {http:#13698554}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer