Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
PHP
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== PHP objects === Basic [[object-oriented programming]] functionality was added in PHP 3 and improved in PHP 4.<ref name="The PHP Group" /> This allowed for PHP to gain further abstraction, making creative tasks easier for programmers using the language. Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.<ref name="mjtsai.com" /> In previous versions of PHP, objects were handled like [[value type]]s.<ref name="mjtsai.com">{{cite web|access-date=2008-03-16|url=http://mjtsai.com/blog/2004/07/15/php-5-object-references/|title=PHP 5 Object References |website=mjtsai.com}}</ref> The drawback of this method was that code had to make heavy use of PHP's "reference" variables if it wanted to modify an object it was passed rather than creating a copy of it. In the new approach, objects are referenced by [[handle (computing)|handle]], and not by value.{{Citation needed|date=November 2023}} PHP 5 introduced private and protected [[member variable]]s and methods, along with [[abstract type|abstract classes]], [[final type|final classes]], [[abstract method]]s, and [[final method]]s. It also introduced a standard way of declaring [[Constructor (computer science)|constructors]] and [[Destructor (computer science)|destructors]], similar to that of other object-oriented languages such as [[C++]], and a standard [[exception handling]] model. Furthermore, PHP 5 added [[Interface (computing)|interfaces]] and allowed for multiple interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. [[object (computer science)|Objects]] implementing ArrayAccess can be used with [[array data type|array]] syntax and objects implementing [[Iterator]] or [[IteratorAggregate]] can be used with the <code>foreach</code> [[language construct]]. There is no [[virtual table]] feature in the engine, so [[static variable]]s are bound with a name instead of a reference at compile time.<ref name="The PHP Group-4">{{cite web|access-date=2008-03-16|url=http://www.php.net/zend-engine-2.php|title=Classes and Objects (PHP 5) |publisher=The PHP Group}}</ref> If the developer creates a copy of an object using the reserved word <code>clone</code>, the Zend engine will check whether a <code>__clone()</code> method has been defined. If not, it will call a default <code>__clone()</code> which will copy the object's properties. If a <code>__clone()</code> method is defined, then it will be responsible for setting the necessary properties in the created object. For convenience, the engine will supply a function that imports the properties of the source object, so the programmer can start with a by-value [[wikt:replica|replica]] of the source object and only override properties that need to be changed.<ref>{{cite web|access-date=2008-03-16|url=https://www.php.net/language.oop5.cloning|title=Object cloning |publisher=The PHP Group}}</ref> The [[Visibility (computer science)|visibility]] of PHP properties and methods is defined using the [[Keyword (computer programming)|keywords]] <code>public</code>, <code>private</code>, and <code>protected</code>. The default is public, if only [[variable (programming)|var]] is used; <code>var</code> is a synonym for <code>public</code>. Items declared <code>public</code> can be accessed everywhere. <code>protected</code> limits access to [[inherited class]]es (and to the class that defines the item). <code>private</code> limits visibility only to the class that defines the item.<ref>{{cite web |url=http://theserverpages.com/php/manual/en/language.oop5.visibility.php |title=Visibility (PHP Manual) |website=theserverpages.com |date=2005-05-19 |access-date=2010-08-26 |archive-date=2010-09-24 |archive-url=https://web.archive.org/web/20100924033414/http://theserverpages.com/php/manual/en/language.oop5.visibility.php |url-status=dead }}</ref> Objects of the same type have access to each other's private and protected members even though they are not the same instance.{{Citation needed|date=November 2023}} ==== Example ==== The following is a basic example of [[object-oriented programming]] in PHP 8: <syntaxhighlight lang="php" line> <?php abstract class User { protected string $name; public function __construct(string $name) { // make first letter uppercase and the rest lowercase $this->name = ucfirst(strtolower($name)); } public function greet(): string { return "Hello, my name is " . $this->name; } abstract public function job(): string; } class Student extends User { public function __construct(string $name, private string $course) { parent::__construct($name); } public function job(): string { return "I learn " . $this->course; } } class Teacher extends User { public function __construct(string $name, private array $teachingCourses) { parent::__construct($name); } public function job(): string { return "I teach " . implode(", ", $this->teachingCourses); } } $students = [ new Student("Alice", "Computer Science"), new Student("Bob", "Computer Science"), new Student("Charlie", "Business Studies"), ]; $teachers = [ new Teacher("Dan", ["Computer Science", "Information Security"]), new Teacher("Erin", ["Computer Science", "3D Graphics Programming"]), new Teacher("Frankie", ["Online Marketing", "Business Studies", "E-commerce"]), ]; foreach ([$students, $teachers] as $users) { echo $users[0]::class . "s:\n"; array_walk($users, function (User $user) { echo "{$user->greet()}, {$user->job()}\n"; }); } </syntaxhighlight> This program outputs the following: {{samp|Students:<br> Hello, my name is Alice, I learn Computer Science<br> Hello, my name is Bob, I learn Computer Science<br> Hello, my name is Charlie, I learn Business Studies<br> Teachers:<br> Hello, my name is Dan, I teach Computer Science, Information Security<br> Hello, my name is Erin, I teach Computer Science, 3D Graphics Programming<br> Hello, my name is Frankie, I teach Online Marketing, Business Studies, E-commerce|style=padding-left:0px; overflow-x: hidden; word-wrap: break-word;}} <!-- Note that there are problems with the script in this section. -->
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
PHP
(section)
Add topic