WordPress custom php classes

What is best practice for writing/using custom PHP classes in a theme?

I want to use a custom PHP class in my theme, however I am not sure where to add it. Is there best practice used by the community for including PHP classes with a theme or is that an individual developer’s decision? The first class I want to add is a custom menu Walker, however there will be more, and they will be tied to my theme. I am split between creating them in separate PHP files versus adding them to functions.php — or maybe there is any other ways please? Many thanks in advance for your input. Katrina

1 Answer 1

Ultimately it’s about maintainability and readability. Functions.php is good for adding just that — functions. Small snippets of code that modify or add to the theme. When you start getting into classes or building some kind of framework, you need to start separating your concerns whether that be moving them into a plugin or simply including your class files into the theme and keeping a well organized directory.

And there’s no performance to contend here — it’s all about what makes development the easiest and most maintainable for you.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Php datetime get hours

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.20.43540

WordPress is a trademark of the WordPress Foundation, registered in the US and other countries. This site is not affiliated with the WordPress Foundation in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Использование PHP-классов при написании плагинов

В этом видеоуроке мы полностью перепишем наш плагин на PHP-класс. Да, до этого мы делали его на обычных функциях.

Это не значит, что любой плагин необходимо писать, используя PHP-классы, вполне возможно, что ваш плагин, состоящий из 5 хуков может вполне прекрасно себя чувствовать на функциях.

Чтобы скачать плагин, который получился в процессе этого урока, нужно купить курс.

Как создать PHP-класс

// не забываем проверять существование класса if( ! class_exists( 'trueMinMaxQty' ) ) { class trueMinMaxQty { // метод-конструктор, который выполнится при инициализации класса public function __construct() { } } // инициализируем класс / создаём объект класса new trueMinMaxQty(); }

Что такое область видимости метода и свойства?

class trueMinMaxQty { // публичный метод-конструктор public function __construct() { } // публичный метод public function MyPublic() { } // защищённый метод protected function MyProtected() { } // приватный метод private function MyPrivate() { } // если не указано, то метод тоже публичный function Foo() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass = new trueMinMaxQty; $myclass->MyPublic(); // ОК $myclass->MyProtected(); // Фатальная ошибка $myclass->MyPrivate(); // Фатальная ошибка $myclass->Foo(); // Все методы отрабатывают норм
  • 11 видеоуроков
  • Можно скачать готовый код после каждого урока
  • Можно начать проходить курс сразу же после оплаты
  • Единоразовый платёж
  • Доступ навсегда
  • Уроки актуальны в 2023-м году

Источник

Time to Upgrade your Browser

☠ Your version of the Chrome browser will unfortunately no longer display websites correctly if it even supports the means of reaching them and is incredibly insecure.

We highly recommend using the Waterfox Classic browser. It’s secure, it will display websites correctly and it’s the only reasonably customizable browser currently available on the market.

Here is the list of currently minimally supported browsers:

  • Gecko Browsers:
    • Waterfox Classic 44.0+, highly customizable browser; learn how to really customize it at the Fixed Firefox website.
    • Waterfox Current 44.0+, based on the current versions of Firefox though without the telemetry.
    • Firefox 44.0+, standard Firefox is no longer customizable though at least it’s not Chrome.
    • Pale Moon 30.0+, second most customizable browser based on earlier versions of Firefox though features slightly less website compatibility than Waterfox Classic.
    • SeaMonkey, based on the original Mozilla Suite that includes e-mail, newsgroup / feed client, IRC chat among other features.
    • Chrome 49.0+, one of the two least recommended browsers due to privacy concerns and security issues such as pass word theft via Google.
    • Chromium 49.0+, Chrome with fewer features.
    • Edge, one of the two least recommended browsers due to privacy concerns and security issues such as pass word theft via Microsoft.
    • Opera, now just another Chrome-clone with minor feature improvements.
    • Vivaldi, semi-customizable browser intended to replace the original/real/discontinued Opera browser.
    • Otter, Otter Browser aims to recreate the best aspects of the classic Opera; works on Linux, Mac and Windows.
    • Safari 10.0+, a semi-customizable browser; compatible versions are now only available on Mac OS.

    We’ll still be here after you’ve updated your computer!

    ⚠ If you are certain that you’re using a modern version of your browser then either stop spoofing your browser’s user agent or at least change the spoofed user agent to something modern and then simply reload the page.

    Источник

Оцените статью