dsjpik057730 2015-01-03 11:44
浏览 12
已采纳

在类别中调用函数,然后调用默认索引()?

I have a class like,

class test
{

  public function index()
  {

  }

  public function home()
  {

  }

}

but when i call my class like,

$test = new test();

It will execute the default function index(), my question is how can i call the function home() and ignore the function index()?

I tried to call the function after making the object of class like $test->home() but it still call the index() first and then call the home().

A little help or direction will be appreciated,

Thanks, Ali

  • 写回答

2条回答 默认 最新

  • doubi4435 2015-01-03 11:59
    关注

    Yes you can do that with adding the magic method __construct() like this:

    <?php
    
        class index {
    
            public function __construct() {
                echo "1";
            }
    
            public function index() {
                echo "2";
            }
    
            public function home() {
                echo "3";
            }
    
        }
    
        $obj = new index();
        $obj->index();
        $obj->home();
    
    ?>
    

    Output:

    123
    

    As you can see every method is called as you want

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题