简体   繁体   English

如何使用PHP类扩展?

[英]How to use PHP class extends?

QUESTION : 题 :

I Cannot figure how to use classes extends in PHP... Even reading php.net website and some examples, there is something I cannot understand or missing ! 我无法弄清楚如何使用PHP中扩展的类...即使阅读php.net网站和一些示例,也有一些我无法理解或遗漏的东西!

Can you please tell me what I'm doing wrong ? 你能告诉我我在做什么错吗?

Api.php Api.php

class Api
{

    public static $action = '';


#    public function __construct()
#    {
#    }

    public function actionCaller ($action,$args=NULL)
    {
        return self::$action_($args);
    }
}

ApiForum.php ApiForum.php

class ApiForum extends Api
{

    #private static $forum;

    public function __construct()
    {
        #self::$forum = new Api();
    }

    private function getPost ($args)
    {
        echo 'executed.';
        #return "get forum post $args";
    }

}

test.php test.php的

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);


require_once('config.php');
require_once('classes/_Autoload_.php');


echo Api::actionCaller('forum')->getPost();

The result : 结果 :

PHP Fatal error:  Call to a member function getPost() on a non-object in /var/www/html/api.example.com/test.php on line 10

Please be clement with me ;) 请和我谦虚;)

CL CL

ANSWER: 回答:

Okay it's working now ! 好吧,现在正在工作! Thanks to all... There was more than one problem, here is the result : 多亏了所有...问题不止一个,这里是结果:

Api.php Api.php

class Api
{

#    public function __construct()
#    {
#    }


    public function actionCaller ($action,$args=NULL)
    {
        return self::$action($args);
    }

    public function forum ()
    {
        return new ApiForum();
    }


}

ApiForum.php ApiForum.php

class ApiForum extends Api
{

#    public function __construct()
#    {
#    }



    public static function getPost ($args)
    {
        echo 'executed.';
    }

}

test.php test.php的

error_reporting(E_ALL);
ini_set('display_errors', true);


require_once('config.php');
require_once('classes/_Autoload_.php');


echo Api::actionCaller('forum')->getPost('test');

I feel I need some more readings about classes and objects scopes ... :) 我觉得我需要更多有关类和对象作用域的内容... :)

Just switch your "getPost" method declaration for this: 只需为此切换“ getPost”方法声明:

static function getPost($args){

A private method means only that class can execute that method. 私有方法意味着只有该类才能执行该方法。 A static method means that it can be called without an object being instantiated, like what you're trying to do with the double colon eg. 静态方法意味着可以在不实例化对象的情况下调用它,例如您尝试使用双冒号进行的操作。 class::method(args) . class::method(args)

Just for completeness sake, a public function is the middle ground. 仅出于完整性考虑,公共职能是中间立场。 An object has to be instantiated for you to call it (via $object->method(args) ), but it is available to any file that has imported that class 必须实例化一个对象以供您调用(通过$object->method(args) ),但是该对象可用于已导入该类的任何文件

EDIT 编辑

Just a side note: I'd also like to add that for a method to be used as a static method, it still needs to be "included"! 附带说明:我还想补充一点,要使方法用作静态方法,仍需要“包含”该方法! I apologise for the use of the word "imported", I've been playing around in a lot of other languages recently! 我为使用“进口”一词表示歉意,最近我还在使用许多其他语言!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM