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_p art-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->keyword s."\">";
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
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_p
$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.'</
}
function DisplayKeywords()
{
echo "<META name =\"keywords\" content=\"".$this->keyword
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
ASKER
I tried that. It didn't work.
Why not use this:
function SetFilename()
{
$this->filename = basename($PHP_SELF,".php") ; // returns "classtest";
}
function SetFilename()
{
$this->filename = basename($PHP_SELF,".php")
}
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.
>
>function SetFilename()
> {
> $this->filename = basename($PHP_SELF,".php")
> }
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.
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-
$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-
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_p art-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->keyword s."\">";
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
Alternative SetFilename() function
-------------------------- ---------- ---
function SetFilename()
{
$brk_filename = explode(".", basename($_SERVER['PHP_SEL F']));
$num_part = count($brk_filename);
if( $num_part >1)
unset($brk_filename[$num_p art-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'].
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_p
$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.'</
}
function DisplayKeywords()
{
echo "<META name =\"keywords\" content=\"".$this->keyword
}
function DisplayFilename()
{
echo $this->filename;
}
}
?>
Alternative SetFilename() function
--------------------------
function SetFilename()
{
$brk_filename = explode(".", basename($_SERVER['PHP_SEL
$num_part = count($brk_filename);
if( $num_part >1)
unset($brk_filename[$num_p
$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
Besides try to avoide register globals set to on.
Here's why:
http://us4.php.net/register_globals
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
$_SERVER['PHP_SELF']