Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Better WordPress Through Objects

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →
Avatar for cogsprocket cogsprocket
November 21, 2011
93

Better WordPress Through Objects

A brief overview of why you should use objects in your PHP and how I use them to make my WordPress better.

Avatar for cogsprocket

cogsprocket

November 21, 2011

Transcript

  1. • I currently do business under the name “Cogsprocket Industries”

    • I’ve done PHP development for nearly 15 years • I’m a contributor to WordPress 3.1.2 “Stubby” - there’s a name no man would self-apply where I come from Monday, November 21, 11
  2. Exhibit A. It's my only exhibit, really, but, you know

    what? I think it's pretty @#&$! good. “global” Monday, November 21, 11
  3. Oh... I probably should have mentioned “scope” first... Variable scope

    is the annoying thing that keeps your functions from seeing values. If you’re annoyed by scope that’s the intended purpose. Monday, November 21, 11
  4. $var OK, what is scope in practice? Scope protects variable

    data function 1 $var function 1 Not the same $var! Monday, November 21, 11
  5. Why this is good Functions are self-contained elements and should

    be. Variable data shouldn’t be modified by just anything. Monday, November 21, 11
  6. My first ex-wife's name is Tammy. My second ex-wife's name

    is Tammy. My Mom's name is Tamara... She goes by Tammy. Problem? Monday, November 21, 11
  7. What are objects? Objects are special code structures that better

    contain data and express intent You’re probably using them already. Objects help organize your project and keep your code DRY. Monday, November 21, 11
  8. Object Code Boot Camp Objects consist of “properties” and “methods”

    Properties are object-specific variables Methods are object-specific functions Monday, November 21, 11
  9. Objects Talk To Each Other Progammer Illustrator Objects communicate through

    properties Objects can call other objects’ methods Monday, November 21, 11
  10. All three of those things are true. The last one

    is only true when things are “public” Monday, November 21, 11
  11. What Does An Object Look Like? class myObject { public

    $publicProperty = “”; prvate $privateProperty = “”; public function __construct() { } private function connect() { } } Monday, November 21, 11
  12. class myObject { public $publicProperty = “”; prvate $privateProperty =

    “”; public function __construct() { } private function connect() { } } Monday, November 21, 11
  13. $myObject = new myObject(); Create objects with “new” Use object

    stuff with -> $myObject->publicProperty = “:D”; Well... public stuff. $myObject->privateProperty = “:_(”; Monday, November 21, 11
  14. Object Properties Must be defined in the class before you

    can use them. If they’re public other objects can read them! (Bye bye “global”) They can be anything a variable can be even another object!!! Monday, November 21, 11
  15. Object Methods Must be defined in the class before you

    can use them. They can alter other object variables (but that object has to be passed to them) They can be anything a function can be and can alter it’s sibling properties. Monday, November 21, 11
  16. It’s like ignoring scope when you’re in an object $this

    gives you object access Monday, November 21, 11
  17. You can only use $this in object scope :( $this

    still has some scope... Monday, November 21, 11
  18. I forgot to mention WordPress... OOP is a big topic

    to tackle My WordPress has OOP Let me show you it. Monday, November 21, 11
  19. require_once("truss/truss.php"); // ------------------------------------------------------- // // Some commonly used components. $truss->customMenu->Name

    = "Main Navigation"; $truss->customMenu->Slug = "main-navigation"; $truss->customMenu->Create(); $truss->widgetArea->Name = "Footer (Left)"; $truss->widgetArea->Description = "Widgets that appear in the footer on the left."; $truss->widgetArea->Create(); $truss->postType->Name = "Tutorials"; $truss->postType->Singular = "Tutorial"; $truss->postType->Create(); This is my functions.php file IT’S TINY! Monday, November 21, 11
  20. class postType { public $Name = ""; // Post type

    name public $Singular= ""; // Post type name (Singular) public $Public = true; // Post type public/private public $Archive = true; // Allow post type to be archived public $Slug = ""; // Set a slug for permalinks public function __construct($_name = 0, $_singular = 0) { $this->registry = $registry; $this->Name = $_name; $this->Singluar = $_singular; } public function Create() { /* Actual WordPress Code */ } } Custom Post Types Monday, November 21, 11
  21. Let’s do some workshoppy stuff... Follow me on Twitter: @cogsprocket

    Confused? I was too. I can help you. Monday, November 21, 11