Home > Backend Development > PHP Tutorial > PHP classes and objects_PHP tutorial

PHP classes and objects_PHP tutorial

WBOY
Release: 2016-07-12 09:08:24
Original
938 people have browsed it

PHP classes and objects

The object model has been completely rewritten since PHP 5 for better performance and more features. This is the biggest change since PHP 4. PHP 5 has a complete object model.​
New features in PHP 5 include access control, abstract and final classes and methods, additional magic methods, interfaces, object copying and type constraints.
PHP treats objects in the same way as references and handles, that is, each variable holds a reference to the object rather than a copy of the entire object.
Attributes:
Variable members of a class are called attributes
Attribute declaration keywords can be public protected private
Variables in attributes can be initialized, but the initialization must be a constant and not a calculation formula
For example
private $name="tom"; //ok
private $name="tom"."jack"; //error
Constant in class:
The value of a constant must be a fixed value and cannot be a variable, class attribute, mathematical operator, function call, etc.
Only const NAME='tom'; can be used to define constants
Access constants use self::NAME inside the class and classname: NAME outside the class
Auto-loading classes:
Case List:
index.php文件
<?php
header("content-type:text/html;charset=utf-8");
function __autoload($className){
require_once $className.&#39;.php&#39;;
}
$obj = new Name();
$obj2 = new User();
var_dump($obj->getName());

Name.php文件
<?php
header("content-type:text/html;charset=utf-8");
class Name{
function getName(){
return "欧阳俊";
}
}

User.php文件
<?php
header("content-type:text/html;charset=utf-8");
class User{
function getUser(){
return array(&#39;ouyangjun&#39;,&#39;jpp&#39;);
}
}
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1054556.htmlTechArticlePHP’s classes and objects have completely rewritten the object model since PHP 5 to get better performance and more features . This is the biggest change since PHP 4. PHP 5 has a complete object model. PHP 5...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template