Traits
- Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
- Traits allow you to reuse several methods freely in different classes, and are a mechanism for code reuse.
trait TraitName { // some code...}
class MyClass {
use TraitName;
}
// ----------------------------------
<?php
// Define a trait
trait message1 {
public function msg1() {
echo "PHP OOP is fun! ";
}
}
// Use the trait in a class
class Welcome {
use message1;
}
$obj = new Welcome();
$obj->msg1();
?>
Static methods and properties
<?php
class pi {
// static property
public static $value = 3.14159;
}
// Get static property directly
echo pi::$value;
?>
<?php
class greeting {
// static method
public static function welcome() {
echo "Hello World!";
}
}
// Call static method directly
greeting::welcome();
?>
The purpose of these lessons is to get a conceptual understanding of PHPUnit testing using WP_PHPUnit for WordPress plugins/themes.
- Composer is a tool package manager for PHP frameworks/libraries like PHPUnit and WP PHPUnit
NPM is a package manager for JS frameworks/libraries.
Wordpress store content in the database.
By default, WordPress only understands a few kinds of content:
- Posts → blog posts
- Pages → static pages (About, Contact)
- Media → images, videos
- Comments
All of these are actually stored in the same database table (
wp_posts) and are distinguished by a field calledpost_type.A Custom Post Type (CPT) is any post type you create yourself instead of using WordPress defaults.
Each one:
- Has its own admin menu
- Has its own URL structure
- Has its own fields/features
- Behaves like content, but not a blog post
When you create a CPT, you choose which WordPress features it uses Examples:`
title→ headline fieldeditor→ main content boxexcerpt→ short summarythumbnail→ featured imagecomments→ comments enabled`
A taxonomy is a system for grouping and classifying content
Default WordPress Taxonomies
WordPress ships with two:
- Category
- Hierarchical (parent → child)
- Example:
- Action
- Superhero
- Spy
- Action
- Tag
- Non-hierarchical (flat)
- Example:
- Marvel
- Oscar
- 2024
- A Custom Taxonomy is any classification system you create yourself. Examples:
- Genre (for Movies)
- Language (for Movies)