php怎么调父类

fiy 其他 94

回复

共3条回复 我来回复
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在 PHP 中,调用父类的方法可以使用 `parent` 关键字。通过 `parent::methodName()` 的形式,可以调用父类中指定的方法。下面是一个示例:

    “`php
    class ParentClass {
    public function printMessage() {
    echo “This is a message from the parent class.”;
    }
    }

    class ChildClass extends ParentClass {
    public function printMessage() {
    // 调用父类的 printMessage 方法
    parent::printMessage();

    // 添加额外的代码
    echo ” This is a message from the child class.”;
    }
    }

    $childObj = new ChildClass();
    $childObj->printMessage();
    “`

    在上面的示例中,`ChildClass` 继承自 `ParentClass`,并且重写了 `printMessage` 方法。在子类的 `printMessage` 方法中,可以使用 `parent::printMessage()` 来调用父类中的方法,并在需要的地方添加额外的代码。

    注意,使用 `parent` 关键字只能调用被子类重写的父类方法,在子类中新增的方法无法通过 `parent` 关键字调用。

    2年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    在PHP中,可以通过调用父类的构造函数和方法来访问或调用父类的属性和方法。下面是一些具体的方法来调用父类:

    1. 使用parent关键字:

    在子类中,可以使用parent关键字来访问父类的构造函数和方法。通过parent关键字,可以调用父类的构造函数和方法,以及访问父类的属性。

    例如,在子类的构造函数中调用父类的构造函数:

    “`php
    class ParentClass {
    protected $name;

    public function __construct($name) {
    $this->name = $name;
    }
    }

    class ChildClass extends ParentClass {
    public function __construct($name) {
    parent::__construct($name);
    }
    }

    $child = new ChildClass(“John”);
    “`

    在这个例子中,子类ChildClass调用了父类ParentClass的构造函数,并传递了一个参数。

    2. 使用parent::方法名:

    在子类中,可以使用parent::方法名的方式来调用父类的方法。通过这种方式,可以访问父类的方法,并且可以传递参数给父类的方法。

    例如,在子类中调用父类的方法:

    “`php
    class ParentClass {
    protected $name;

    public function sayHello() {
    echo “Hello, ” . $this->name;
    }
    }

    class ChildClass extends ParentClass {
    public function callParentMethod() {
    parent::sayHello();
    }
    }

    $child = new ChildClass();
    $child->callParentMethod();
    “`

    在这个例子中,子类ChildClass通过调用parent::sayHello()来调用父类的sayHello方法,并且在子类中调用callParentMethod方法即可调用父类的方法。

    3. 使用parent::属性名:

    在子类中,可以使用parent::属性名的方式来访问父类的属性。通过这种方式,可以访问父类的属性,并且可以修改父类的属性。

    例如,在子类中访问父类的属性:

    “`php
    class ParentClass {
    protected $name = “John”;

    public function getName() {
    return $this->name;
    }
    }

    class ChildClass extends ParentClass {
    public function showParentName() {
    echo parent::$name;
    }
    }

    $child = new ChildClass();
    $child->showParentName();
    “`

    在这个例子中,子类ChildClass通过调用parent::$name来访问父类的属性,并且在子类中调用showParentName方法即可访问父类的属性。

    4. 使用parent关键字和self关键字:

    在子类中,可以同时使用parent关键字和self关键字来访问父类和子类的方法。通过这种方式,可以在子类中调用父类的方法,并且在父类的方法中调用子类的方法。

    例如,在父类中调用子类的方法:

    “`php
    class ParentClass {
    protected $name;

    public function __construct($name) {
    $this->name = $name;
    $this->sayHello();
    }

    public function sayHello() {
    echo “Parent says hello!”;
    }
    }

    class ChildClass extends ParentClass {
    public function sayHello() {
    echo “Child says hello!”;
    }
    }

    $child = new ChildClass(“John”);
    “`

    在这个例子中,父类ParentClass的构造函数调用了sayHello方法,并且在子类ChildClass中重写了sayHello方法。当子类ChildClass实例化时,父类的构造函数会被调用,并且会输出 “Child says hello!”。

    5. 使用parent::方法名和self::方法名:

    在子类中,可以同时使用parent::方法名和self::方法名来访问父类和子类的方法。通过这种方式,可以在子类中调用父类的方法,并且在父类的方法中调用子类的方法。

    例如,在父类中调用子类的方法:

    “`php
    class ParentClass {
    protected $name;

    public function __construct($name) {
    $this->name = $name;
    $this->sayHello();
    }

    public function sayHello() {
    self::greet();
    }

    protected function greet() {
    echo “Parent says hello!”;
    }
    }

    class ChildClass extends ParentClass {
    protected function greet() {
    echo “Child says hello!”;
    }
    }

    $child = new ChildClass(“John”);
    “`

    在这个例子中,父类ParentClass的构造函数调用了sayHello方法,并且在父类的sayHello方法中调用了self::greet方法。在子类ChildClass中重写了greet方法。当子类ChildClass实例化时,父类的构造函数会被调用,并且会输出 “Child says hello!”。

    总结:

    通过上述方法,可以在子类中调用父类的构造函数和方法,访问父类的属性,并且在父类的方法中调用子类的方法。这些方法提供了灵活性和可扩展性,使得子类能够继承和重写父类的功能,并且可以根据具体的业务需求进行调整和扩展。

    2年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在PHP中,我们可以使用parent关键字来调用父类的方法和属性。通过调用父类的方法,我们可以在子类中重用父类的功能,并且在子类中添加或修改特定的行为。

    调用父类的方法有两种方法:使用parent关键字和使用类名。下面我们将对这两种方法进行详细介绍。

    使用parent关键字调用父类的方法
    要调用父类的方法,我们可以使用parent关键字,后跟双冒号运算符和要调用的方法名。例如:

    class ParentClass {
    public function myMethod() {
    echo “This is the parent method.”;
    }
    }

    class ChildClass extends ParentClass {
    public function myMethod() {
    parent::myMethod(); // 调用父类的方法
    echo “This is the child method.”;
    }
    }

    $child = new ChildClass();
    $child->myMethod();
    在这个例子中,ParentClass是父类,ChildClass是子类。子类重写了父类的myMethod方法,并使用parent::myMethod()调用父类的方法。这样,当我们调用子类的myMethod方法时,会首先执行父类的方法,然后再执行子类的方法。

    使用类名调用父类的方法
    除了使用parent关键字,我们还可以使用类名来调用父类的方法。例如:

    class ParentClass {
    public static function myMethod() {
    echo “This is the parent method.”;
    }
    }

    class ChildClass extends ParentClass {
    public static function myMethod() {
    ParentClass::myMethod(); // 调用父类的方法
    echo “This is the child method.”;
    }
    }

    ChildClass::myMethod();
    在这个例子中,我们定义了一个静态方法myMethod。子类ChildClass中使用ParentClass::myMethod()调用父类的方法。这将直接调用父类的方法,而不需要创建父类的实例。

    在以上两种方法中,我们都可以调用父类的公共方法或受保护方法。但是,如果父类的方法是私有的,无法在子类中直接调用。

    总结
    通过使用parent关键字或类名,我们可以在子类中调用父类的方法。这样可以实现代码的重用,并在子类中添加或修改特定的行为。使用parent关键字主要适用于非静态方法,而使用类名调用适用于静态方法。无论选择哪一种方法,都能够在子类中调用父类的方法。

    2年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部