Классы в PHP 8

Contents
Пример
__construct
Похожие статьи

Пример

Пример реализации класса

<?php { class Page const LANG = 'ru'; public function setPageLang($page_lang = null) { $this->page_lang = $page_lang; if($page_lang) { else { } } $this->page_lang = self::LANG; } public function getPageLang() { return $this->page_lang; } } $page = new Page; $page->setPageLang('en'); echo $page->getPageLang();

en

__construct()

__construct() - это один из так называемых магических методов (magic methods)

Его не нужно вызывать отдельно - он автоматически вызывается каждый раз когда вы создаете новый объект класса с помощью new

Рассмотрим применение __construct() для создания страницы Парк Палома в Бенальмадене

<?php class Page { public function __construct( $lang, $title, $description, $keywords, ) { $this->description = $description; $this->keywords = $keywords; $this->lang = $lang; $this->title = $title; } public function create_head() { return' <head> <title>'.$this->title.'</title> <meta name="description" content="'.$this->description.'"> <meta name="keywords" content="'.$this->keywords.'"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="/.css/heihei_style.css"> </head> '; } }

Теперь подключим этот класс к файлу Main.php

<?php require_once 'Page.php'; $page = new Page( lang: "ru", title: "Парк Палома в Бенальмадене", description: "Бесплатный парк с птицами и черепахами Коста-дель-соль", keywords: "Парк Палома, Бенальмадена парк с животными, Бенальмадена пруд с черепахами" ); echo $page->create_head();

Запустим Main.php

<head> <title>Парк Палома в Бенальмадене</title> <meta name="description" content="Бесплатный парк с птицами и черепахами Коста-дель-соль"> <meta name="keywords" content="Парк Палома, Бенальмадена парк с животными, Бенальмадена пруд с черепахами"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="/.css/heihei_style.css"> </head>

Related Articles
Development with PHP
Arrays in PHP
Date and Time in PHP - basics
How to display time of multiple timezones in PHP
How to add variable to url in PHP
json_decode
How to get screen size with PHP
Call function from another file
Premature end of chunk coded message body: closing chunk expected
Generate unique random numbers with PHP
Check your HTTP_USER_AGENT
Compare two dates
PHP 8 Classes
Comments in PHP
Cookies
PHP sessions
Authentication and Authorization in PHP 8

Search on this site

Subscribe to @aofeed channel for updates

Visit Channel

@aofeed

Feedback and Questions in Telegram

@aofeedchat