How to access the parent instance of a class in PHP?

The only object-oriented programming experience I have is C #, so PHP throws me some crooked balls that I could use.

I have a class that I use for all my pages, "pagebase", if you do. It processes the lowest level html structure. This class is inherited by several other classes. These classes are different types of pages that are on the site. Now: it's hard for me to set the variable to "pagebase" from an instance of the class that inherits it. In C #, that won't be a problem, since the class instance behaves as if it were an inherited class.

This is a view of what I have:

pagebase.php

<?php
    class pagebase
    {
        var $title = "No title";
        var $body = "<center>No content</center>";

        function setTitle($value) {
            $this->title = $value;
        }

        function setBody($value) {
            $this->title = $value;
        }

        function updateHTML()
        {
            ...
        }

        function drawPage()
        {
            $this->updateHTML();
            echo $this->html;
        }
    }
?>

std_page.php

<?php
    include("includes/pagebase.php");

    class std_page extends pagebase
    {
        function std_page()
        {
            ...
        }

        function updateHTML()
        {
            parent::setBody(
                "
                    <div id=\"main_wrapper\">
                        The page goes here!
                    </div>
                "
            );
        }

        function drawPage()
        {
            $this->updateHTML();
            parent::drawPage();
        }
    }
?>

index.php

<?php
    include "includes/std_page.php";

    $page = new std_page;
    $page->setTitle("Avesta");

    $page->drawPage();
?>

, , , . , , , , .

-, , , -

+3
3

, var, , accesibilty, public, private protected.

, , $this. , $this->title, $this->setTitle('title')

static, , , ::. , public static $title, , pagebase::$title

.

+2

parent, , ::.

, $this->setBody .

0

, . , ...

abstract class pagebase
{
    private $title = 'No title';
    private $body = 'No content';

    public function setTitle($value) {
        $this->title = $value;
    }

    public function setBody($value) {
        $this->body = $value;
    }

    public function drawPage()
    {
        $this->updateHTML();
        echo $this->body;
    }

    abstract protected function updateHTML();
}

class std_page extends pagebase
{
    protected function updateHTML()
    {
        $this->setBody(
            "
                <div id=\"main_wrapper\">
                    The page goes here!
                </div>
            "
        );
    }
}

:

  • pagebase abstract, updateHTML . pagebase, , , updateHTML.
  • private, , ( , std_page) . , protected. public. . visibility .
  • , , parent::. , , , . , - , . . № 3 .
  • If you do not change the way you work drawPage, there is no need to redefine the method. As you overload updateHTML, this overloaded version is automatically used in the existing implementation drawPage.
  • I fixed the copy and paste error where your method setBodyreally set yours title, and you used it $htmlin some places where you supposedly meant it $body.
0
source

Source: https://habr.com/ru/post/1763263/


All Articles