PHP Object-Oriented Programming Flashcards
(25 cards)
What is Object-Oriented Programming (OOP) in PHP?
OOP is a programming paradigm that uses objects and classes to organize code.
OOP promotes modularity and reuse through encapsulation, inheritance, and polymorphism. In WordPress, OOP is used in plugins like WooCommerce. Freelancers build maintainable plugins, while enterprise architects design scalable systems with OOP principles.
What is a class in PHP?
A class is a blueprint for objects, defined with class MyClass {}.
Classes define properties and methods. In WordPress, classes manage complex logic (e.g., WC_Product). Freelancers create classes for custom functionality, while enterprise architects use them for structured, reusable code.
What is an object in PHP?
An object is an instance of a class, created with new, like $obj = new MyClass();.
Objects hold data and behavior. In WordPress, objects represent posts or users. Freelancers instantiate objects for client features, while enterprise architects manage object lifecycles in large systems.
What is a property in a PHP class?
A property is a variable defined in a class, like public $name;.
Properties store object data. In WordPress, properties hold post attributes (e.g., $post->ID). Freelancers use properties for dynamic data, while enterprise architects ensure proper access control.
What is a method in a PHP class?
A method is a function defined in a class, like public function getName() {}.
Methods define object behavior. In WordPress, methods handle tasks (e.g., WC_Cart->add_to_cart()). Freelancers create methods for plugin logic, while enterprise architects design reusable methods.
What is the public access modifier in PHP?
public allows unrestricted access to a property or method.
For example, public $name; is accessible anywhere. In WordPress, public methods are common in APIs. Freelancers use public for client-facing features, while enterprise architects limit exposure for security.
What is the private access modifier in PHP?
private restricts access to within the class, like private $data;.
Prevents external access, used in WordPress for sensitive data. Freelancers protect internal logic, while enterprise architects enforce encapsulation for robust systems.
What is the protected access modifier in PHP?
protected allows access within the class and its subclasses, like protected $value;.
Used in WordPress for inheritance (e.g., plugin base classes). Freelancers use it for extendable code, while enterprise architects ensure controlled access in hierarchies.
What is a constructor in PHP?
A constructor is a special method (__construct()) called when an object is created.
For example, public function __construct($name) { $this->name = $name; }. In WordPress, constructors initialize objects (e.g., WC_Order). Freelancers set up objects, while enterprise architects optimize initialization.
What is a destructor in PHP?
A destructor (__destruct()) runs when an object is destroyed.
For example, public function __destruct() { unset($this->data); }. Rarely used in WordPress, freelancers clean up resources, while enterprise architects manage memory in high-load systems.
What is the $this keyword in PHP?
$this refers to the current object, like $this->name.
Used to access properties/methods within a class. In WordPress, $this is common in plugin classes. Freelancers use it for object context, while enterprise architects ensure proper usage to avoid errors.
What is inheritance in PHP?
Inheritance allows a class to extend another using extends, like class Child extends Parent {}.
In WordPress, plugins inherit base classes (e.g., WC_Product_Variable). Freelancers extend functionality, while enterprise architects design hierarchical, reusable codebases.
What is method overriding in PHP?
Method overriding redefines a parent class method in a child class.
For example, class Child extends Parent { public function myMethod() { /* new logic */ } }. In WordPress, it customizes plugin behavior. Freelancers override methods for clients, while enterprise architects ensure compatibility.
What is the parent keyword in PHP?
parent accesses a parent class’s methods, like parent::myMethod();.
Used in WordPress to call overridden methods. Freelancers use it for extension, while enterprise architects maintain parent-child consistency in complex hierarchies.
What is an interface in PHP?
An interface defines methods a class must implement, like interface MyInterface { public function myMethod(); }.
In WordPress, interfaces ensure plugin compatibility. Freelancers implement interfaces for standardization, while enterprise architects use them for contract-driven development.
How do you implement an interface in PHP?
Use implements, like class MyClass implements MyInterface { public function myMethod() {} }.
Ensures method implementation. In WordPress, interfaces are used in APIs. Freelancers follow interface contracts, while enterprise architects enforce them for system interoperability.
What is an abstract class in PHP?
An abstract class cannot be instantiated and may include abstract methods, like abstract class MyClass { abstract public function myMethod(); }.
In WordPress, abstract classes provide base functionality (e.g., WooCommerce). Freelancers extend them, while enterprise architects use them for shared logic in large systems.
What is a static property in PHP?
A static property belongs to the class, not an object, like public static $count = 0;.
Accessed with MyClass::$count. In WordPress, static properties track global states. Freelancers use them for counters, while enterprise architects manage static data carefully.
What is a static method in PHP?
A static method is called on the class, like public static function myMethod() {}.
Accessed with MyClass::myMethod(). In WordPress, static methods are used in utilities. Freelancers use them for helper functions, while enterprise architects ensure stateless design.
What is the self keyword in PHP?
self refers to the current class, like self::$count;.
Used for static properties/methods. In WordPress, self is common in plugin classes. Freelancers use it for class-level access, while enterprise architects prefer self over hardcoding class names.
What is polymorphism in PHP?
Polymorphism allows objects of different classes to be treated as a common type.
Achieved via interfaces or inheritance. In WordPress, plugins use polymorphism for extensible APIs. Freelancers design flexible code, while enterprise architects leverage it for modular systems.
What is a trait in PHP?
A trait is a reusable set of methods included with use, like trait MyTrait { public function myMethod() {} }.
In WordPress, traits share code across classes. Freelancers use them for reusable logic, while enterprise architects avoid trait conflicts in large codebases.
How do you use a trait in PHP?
Use the use keyword in a class, like class MyClass { use MyTrait; }.
Traits add methods without inheritance. In WordPress, they’re used in plugins for shared functionality. Freelancers simplify code, while enterprise architects ensure trait compatibility.
What is method chaining in PHP?
Method chaining calls multiple methods in sequence, like $obj->method1()->method2();. Explanation: Requires methods to return $this. In WordPress, chaining is used in APIs (e.g., WP_Query). Freelancers use it for fluent interfaces, while enterprise architects design chainable APIs.