PHP OOP
DONE $object = new randClass; $object->names = ['John', 'Billy', 'Susan'];
Go create an object based on some random class, and add two properties two it: one String, one Array Then output the array
DONE
Go create two classes; one with a constructor that accepts an object of the other. (use the new keyword in the instantiate) Base it on a Lock and Chest mechanic, and all the stuff locks and chests can do.
DONE
Go make a class that implements a Trait which has functions
DONE
Go write a method that calls a parent
DONE
Go write a static method and invoke it
yes; they use different code echo $obj->bar; //echos out bar variable echo $obj->bar(); //echos out bar method
can you have same named objects and variables?
NO
can you instantiate a Trait?
$x = new SimpleClass();
consider a PHP class called SimpleClass how would you create an instance of it?
function adder(&$myVar){ $myVar++; }
consider function 'adder' that takes a variable by reference, and increases it by 1
$this is defined (A) $this is not defined. $this is defined (B) $this is not defined. // the object is only defined if it is invoked by an object, and not straight from the class
describe the output
properties
fields are to Java, as ______ are to PHP
self::$property
how are static properties accessed within a class?
$foo = 'SimpleClass'; $bar = new $foo(); //new SimpleClass()
how can a class instance be created using a string?
1. put list of require_once 'classes/asdf.php' in a .php file 2. then require_once that .php file
how can you include a bunch of interfaces/classes in one line of code?
require_once 'classes/Calculator.php'; require_once 'classes/Adder.php'; require_once 'classes/Subtractor.php'; require_once 'classes/Divider.php';
how do you include interfaces or classes manually?
echo MyClass::CONSTANT; $x = new MyClass(); echo $x::CONSTANT; $x->showConstant();
how would you call the constant without creating an object? what are the two ways you can output the constant using an object?
class BaseClass{ function __constructor(){ echo "I just made a BaseClass object!"; } }
how would you create a default constructor to a class?
class SubClass{ function __construct(){ parent::__construct(); } }
how would you invoke a constructor from a parent class, within a constructor of a subclass?
parent::myMethod();
how would you invoke a method from a parent within a subclass?
class BaseClass{ function __construct(){ echo "I just made a BaseClass object!"; } function __construct($a1){ echo "I just made a BaseClass object, with " . $a1; } function __construct($a1, $a2){ echo "I just made a BaseClass object, with " . $a1 . " and " . $a2; }
how would you overload constructors in PHP?
class Talker { use A, B { B::scream insteadof A; A::shout insteadof B; } }
if two Traits hold the same named method, what code can you use to clarify what to use?
an instance of 'this' class
what can $this also be known as?
(static) properties and (static) functions; or abstract functions (that need to be implemented by subclass)
what can a Trait bring to a class?
all of the public and protected methods
what does a subclass inherit from its parent?
horizontal composition of behavior; that is, the application of class members without requiring inheritance.
what does a trait allow?
makes them accessible without needing an instantiation of the class
what does declaring properties or methods as static do?
$x becomes a reference of $y (points to the same location in the memory)
what does the following code do? $x =& $y;
scope resolution operateor; allows access to static, constant, and overridden properties or methods of a class
what is ::? what does it do?
a method called from an object when that object isn't used anymore in the code; function __destruct(){ echo "Destroying " . $this->name; }
what is a destructor method, and what is its syntax?
language assisted copy and paste
what is a good way at looking at Traits as?
one PHP source file per class definition
what is the convention for defining classes and files?
spl_autoload_register(function($myClass) { require_once "classes/{$myClass}.php"; }); //whenever a class is created, it finds it
what is the spl_autoload_register way of adding a bunch of classes to a new file?
$myObject->objectMethod();
what is the standard way of calling a method from an object?
$obj->bar();
what is the standard way of calling a variable from an object?
require 'myFile.php';
what is the syntax for adding a file to a script?
must be called from an object
what must be true in order for $this to work?
extends
what word creates a subclass of a class?
$this->property (where property is the name of the property)
within class methods, how are non-static properties accessed by?