In object-oriented programming (English: Object-oriented programming, abbreviation: OOP), an object is a whole composed of information and a description of how to process the information. It is an abstraction of the real world.
In the real world, the things we face are objects, such as computers, televisions, bicycles, etc.
The main three characteristics of the object:
The behavior of the object: what operations can be applied to the object, turning on the light and turning off the light are behaviors.
The shape of the object: how the object responds, color, size, and appearance when those methods are applied.
Representation of objects: The representation of objects is equivalent to an ID card. It specifically distinguishes the differences between the same behaviors and states.
This course teaches the basic concepts of object-oriented and related case practices, so that students can have a basic understanding of object-oriented and master the method of abstracting practical problems into class objects to solve practical problems. , master the most important core capabilities of object-oriented.
Video playback address: http://www.php.cn/course/329.html
Difficulties in this video:
1. __construct:
Built-in constructor, automatically called when the object is created. See the following code:
<? php classConstructTest { private $arg1; private $arg2; public function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } public function printAttributes() { print '$arg1 = ' . $this->arg1 . ' $arg2 = ' . $this->arg2 . "\n"; } } $testObject = new ConstructTest("arg1", "arg2"); $testObject->printAttributes();
The running result is as follows:
Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2
2. parent:
is used to directly call methods in the parent class in the subclass, and its function is equivalent to super in Java.
<? php classBaseClass { protected $arg1; protected $arg2; function __construct($arg1, $arg2) { $this->arg1 = $arg1; $this->arg2 = $arg2; print "__construct is called...\n"; } function getAttributes() { return '$arg1 = ' . $this->arg1 . ' $arg2 = ' . $this->arg2; } } class SubClass extends BaseClass { protected $arg3; function __construct($baseArg1, $baseArg2, $subArg3) { parent::__construct($baseArg1, $baseArg2); $this->arg3 = $subArg3; } function getAttributes() { return parent::getAttributes() . ' $arg3 = ' . $this->arg3; } } $testObject = new SubClass("arg1", "arg2", "arg3"); print $testObject->getAttributes() . "\n";
The running results are as follows:
Stephens-Air:Desktop$ php Test.php
__construct is called...
$arg1 = arg1 $arg2 = arg2 $arg3 = arg3
The above is the detailed content of PHP object-oriented programming video material sharing. For more information, please follow other related articles on the PHP Chinese website!