?
Solved

using PHP_SELF in a class

Posted on 2005-04-03
12
Medium Priority
?
515 Views
Last Modified: 2012-08-13
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;
      }
}
?>
0
Comment
Question by:pacidev
[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
12 Comments
 
LVL 6

Expert Comment

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

$_SERVER['PHP_SELF']
0
 
LVL 2

Author Comment

by:pacidev
ID: 13696707
I tried that. It didn't work.
0
 
LVL 4

Expert Comment

by:JKlatte
ID: 13696859
Why not use this:

 function SetFilename()
     {
          $this->filename = basename($PHP_SELF,".php"); // returns "classtest";
     }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:pacidev
ID: 13697023
>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.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 13697114
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-
0
 
LVL 2

Author Comment

by:pacidev
ID: 13697541
>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'].

0
 
LVL 6

Expert Comment

by:peyox
ID: 13697596
That was the good one :)
Besides try to avoide register globals set to on.
Here's why:
http://us4.php.net/register_globals
0
 
LVL 2

Author Comment

by:pacidev
ID: 13697784
>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.
0
 
LVL 6

Accepted Solution

by:
peyox earned 300 total points
ID: 13697957
I would recommend you to go to php.ini and set register_globals to OFF, this will avoid future problems when running your scripts on servers with newer versions of PHP (when register_globals is set to OFF).

Since $PHP_SELF is working for you, you have it set to ON (no good...)

It is good practice to use $_POST[], $_GET[], $_SERVER[] etc. collections instead of global variables.

Hope this helps




0
 
LVL 3

Assisted Solution

by:lokip
lokip earned 300 total points
ID: 13698554
Instead of explodiing and imploding, it is easier to do:

$filename = substr( $file, 0, strrpos( $file, "." ) - 1 );

Register globals needs to be OFF! I would guess 90% of webhosts now have it set to off, and it is slated to be completely removed in future versions. Register globals is evil and just adds overhead anyway.
0
 
LVL 14

Expert Comment

by:huji
ID: 15983488
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 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