PHP Object-oriented-class

Source: Internet
Author: User

<?php
/**
* Created by Phpstorm.
* user:63448
* DATE:2018/5/6
* time:9:44
*/
Class: Encapsulation class for similar data and data operations
Member Volume: The average amount plus a certain amount of modification becomes the member volume Public,protected,private
Member methods: Ordinary functions, with a certain amount of modification, are put into the class and become member methods.
Instantiation: The process of creating a special case for a class (instantiating an object new)
Object: Special case of class or instance object
Understanding objects such as the encapsulation of classes--




/**
* Instantiation Class (instance):
* Object variable = new class name;
* or object variable = new class name ();
* Object variable: Normal variable, except that his type is object type
* $object = new myclass;/new myClass ();
*/
Class myclassss{}//does not have parentheses when declaring classes (as distinct from functions)
$obj = instantiation of the new myclassss;//class
if (class_exists ("Myclassss")) {
if (Is_object ($obj)) echo "obj is Object";//is_object (): Determines whether a variable is an object;
else echo "obj IsNot object";
}else{
echo "MyClass IsNot exists";
}
echo "
/**
* Member variables:
A normal variable is put into a class and becomes a member (a property of a class)
Format: modifier variable name [= value];
Variable definition format: modifier variable;
Assignment of variables: modifier variable name = value;
Modifier: Public: Common, can be used within the class, can also be used outside the class, equivalent to JS var
Public $height = 100;
* Member Method:
The normal function is put into a class that becomes a member method
Format:
Modifier function Method Name (parameter) {
return value
}
*/
Class use steps
Class myclass{
Public $age = 100;
}
$obj = new MyClass;
Echo $obj, age; 100
$obj, age = 105; 105
Echo $obj, age;
The independence of object manipulation
$obj 1 = new MyClass ();
Echo $obj 1, age; 100
$obj 1, age = 200;
Echo $obj 1, age; 200
Echo $obj, age; 105
echo "<br>----------------------------------------<br>";

/**
* Out-of-class use: Object name, variable name (no $)--$obj--Checklogin ()
In-class invocation: Variable name $this--(No $) pseudo variable--$this tag
Pseudo variables:
$this: The object itself
3) for the operation of an object, there is no effect on the other object, that is, the so-called object-Independent
*/

Class usermodel{
Public $user = "admin";
Public $pwd = "123456";
Public $tag = 0; Whether the user is logged in
Incoming user logged-on data
Public Function Setuserpwd ($u, $p) {
$this, Login ($u, $p);
}
User logon action
Public Function Login ($tmp _u, $tmp _p) {
if (! $this, tag) {
if ($this user = = $tmp _u && $this, pwd = = $tmp _p)
$this, tag = 1; User is logged in status
}
}
Determine if the user is logged on
Public Function Checklogin () {
if ($this, tag = = 1) return true;
else return false;
}
}
$obj = new Usermodel;
$obj-Setuserpwd ("admin", "123456");
if ($obj, Checklogin ())
echo "Login OK";
Else
echo "Login Error";


echo "<br>----------------------------------------<br>";

/**
* 1,:: Called the scope operator
* 2, constants must be assigned at the time of declaration!
* 3, in the process of declaring constants, no other modifiers are required
*/
Class myclasss{
Const host= "localhost";//Constant amount format in a class: Const uppercase constant name = value;
Const user= "root";
Public function Show () {
Echo myclasss::host;
echo self::user;//within class: Self (refers to this class) self:: constant name
}
}
Echo Myclasss::host. myclasss::user;//out-of-class invocation: Class Name:: Constant Name
echo "$obj = new Myclasss;
$obj, show ();

Encapsulation of classes:

?>

PHP Object-oriented-class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.