Procedural
Object-Oriented
<?php
// The blueprint
class FormSubmission {
public function validate() {
// some activity
}
}
// The object (implementation)
$form_submission = new FormSubmission();
// Another object
$another_submission = new FormSubmission();
<?php
class FormSubmission {
public function __construct() {
$this->validate();
}
public function validate() {
// some activity
}
}
$form_submission = new FormSubmission();
<?php
class Thing {
public function foo() {
echo "method foo";
}
public function biz() {
$this->bar();
}
protected function bar() {
echo "method bar";
}
}
$thing = new Thing();
$thing->foo(); // => method foo
$thing->biz(); // => method bar
// => E_ERROR : type 1 -- Call to protected method Thing::bar()...
$thing->bar();
<?php
class Thing {
protected function foo() {
echo "method foo";
}
private function bar() {
echo "method foo";
}
}
class AnotherThing extends Thing {}
$thing = new AnotherThing();
$thing->foo(); // => method foo
$thing->bar(); // => E_ERROR : type 1 -- Call to protected method Thing::foo()...
<?php
class Thing {
public static $somevar = "some variable";
public static function foo() {
echo "grabbing " . self::$somevar;
}
}
echo Thing::$somevar; // some variable
Thing::foo(); // => grabbing some variable
<?php
class FormSubmission {
protected $is_valid;
public function __construct($somedata) {
$this->validate($somedata);
}
public function validate($data) {
// do some validation
$this->is_valid = $data . " is good to go";
}
public function is_valid() {
return $this->is_valid;
}
}
$submission = new FormSubmission('some input');
echo $submission->is_valid(); // => some input is good to go