在非对象问题上再次调用成员函数 get_segment()

发布于 2024-09-03 19:37:16 字数 3619 浏览 4 评论 0原文

调用此代码时出现上述错误:

<?
class Test1 extends Core {
function home(){
?>
This is the INDEX of test1
<?
}
function test2(){
echo $this->uri->get_segment(1); //this is where the error comes from
?>
This is the test2 of test1 testing URI
<?
}
}
?>

我在注释处收到错误。

这个类扩展了这个类:

<?php
class Core {

public function start()
{
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

}elseif($this->uri->get_segment(1) and ! file_exists("funk/pages/".$this->uri->get_segment(1).".php")){

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

这是 uri.php 的内容:

<?php
class uri
{
    private $server_path_info = '';
    private $segment = array();
    private $segments = 0;

    public function __construct()
    {
        $segment_temp = array();
        $this->server_path_info = preg_replace("/\?/", "", $_SERVER["PATH_INFO"]);
        $segment_temp = explode("/", $this->server_path_info);
        foreach ($segment_temp as $key => $seg)
        {
            if (!preg_match("/([a-zA-Z0-9\.\_\-]+)/", $seg) || empty($seg)) unset($segment_temp[$key]);
        }
        foreach ($segment_temp as $k => $value)
        {
            $this->segment[] = $value;
        }
        unset($segment_temp);
        $this->segments = count($this->segment);
    }

    public function segment_exists($id = 0)
    {
        $id = (int)$id;
        if (isset($this->segment[$id])) return true;
        else return false;
    }

    public function get_segment($id = 0)
    {
    $id--;
        $id = (int)$id;
        if ($this->segment_exists($id) === true) return $this->segment[$id];
        else return false;
    }
}
?>

我之前问过类似的问题,但答案不适用于此处。

我已经重写了我的代码 3 次来杀死并删除这个被遗弃的错误!但不……

编辑: 我已经更改了核心类以添加 __construct:

<?php
class Core {
function __construct(){
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();
}

public function start()
{

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

}elseif($this->uri->get_segment(1) and ! file_exists("funk/pages/".$this->uri->get_segment(1).".php")){

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

但现在它说: Cannot redeclare class uri in /home/afsadad/public_html/private/funkyphp/funk/funks/libraries/uri.php on line 3

I get the above error when calling this code:

<?
class Test1 extends Core {
function home(){
?>
This is the INDEX of test1
<?
}
function test2(){
echo $this->uri->get_segment(1); //this is where the error comes from
?>
This is the test2 of test1 testing URI
<?
}
}
?>

I get the error where commentated.

This class extends this class:

<?php
class Core {

public function start()
{
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

}elseif($this->uri->get_segment(1) and ! file_exists("funk/pages/".$this->uri->get_segment(1).".php")){

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

And here is the contents of uri.php:

<?php
class uri
{
    private $server_path_info = '';
    private $segment = array();
    private $segments = 0;

    public function __construct()
    {
        $segment_temp = array();
        $this->server_path_info = preg_replace("/\?/", "", $_SERVER["PATH_INFO"]);
        $segment_temp = explode("/", $this->server_path_info);
        foreach ($segment_temp as $key => $seg)
        {
            if (!preg_match("/([a-zA-Z0-9\.\_\-]+)/", $seg) || empty($seg)) unset($segment_temp[$key]);
        }
        foreach ($segment_temp as $k => $value)
        {
            $this->segment[] = $value;
        }
        unset($segment_temp);
        $this->segments = count($this->segment);
    }

    public function segment_exists($id = 0)
    {
        $id = (int)$id;
        if (isset($this->segment[$id])) return true;
        else return false;
    }

    public function get_segment($id = 0)
    {
    $id--;
        $id = (int)$id;
        if ($this->segment_exists($id) === true) return $this->segment[$id];
        else return false;
    }
}
?>

i have asked a similar question to this before but the answer does not apply here.

I have rewritten my code 3 times to KILL and Delimb this godforsaken error! but nooooooo....

EDIT:
I have changed the core class to add __construct:

<?php
class Core {
function __construct(){
require("funk/funks/libraries/uri.php");
$this->uri = new uri();
require("funk/core/loader.php");
$this->load = new loader();
}

public function start()
{

if($this->uri->get_segment(1) != "" and file_exists("funk/pages/".$this->uri->get_segment(1).".php")){
include("funk/pages/". $this->uri->get_segment(1).".php");
$var = $this->uri->get_segment(2);
if ($var != ""){
$home= $this->uri->get_segment(1);
$Index= new $home();
$Index->$var();


}else{
$home= $this->uri->get_segment(1);
$Index = new $home();
$Index->home();

}

}elseif($this->uri->get_segment(1) and ! file_exists("funk/pages/".$this->uri->get_segment(1).".php")){

echo "404 Error";

}else{
include("funk/pages/index.php");
$Index = new Index();
$Index->home();
//$this->Index->index();


echo "<!--This page was created with FunkyPHP!-->";
}
}
}
?>

but now it says: Cannot redeclare class uri in /home/afsadad/public_html/private/funkyphp/funk/funks/libraries/uri.php on line 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

机场等船 2024-09-10 19:37:16

调用成员时

$this->uri->get_segment(1);

$this->uri不是对象;可能根本没有定义。我猜你的对象定义 $this->uriCore::start() 方法在 Test1::test() 之前没有被调用代码>.也许您想在构造函数 __construct() 中初始化它?

When calling

$this->uri->get_segment(1);

the member $this->uri is not an object; probably it is not defined at all. I guess that your object's Core::start() method that defines $this->uri is not called prior to Test1::test(). Perhaps you wanted to initialize it in the constructor, __construct()?

久伴你 2024-09-10 19:37:16

您需要第三行的类文件...使用 require_once() 或类自动加载函数。

You are requiring a classfile on line three... use require_once(), or a class autoloading function.

余罪 2024-09-10 19:37:16
$CI =& get_instance();
echo $CI->uri->segment(1);
$CI =& get_instance();
echo $CI->uri->segment(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文