Definition of classes and creation and access of objects

Source: Internet
Author: User
Tags array definition integer naming convention php file

Definition of Class

Class Name {

member properties (variables)

}

Note: The first letter of the class name should be capitalized.

A member variable is an integral part of a class, typically a basic data type (an integer, a string), or a complex data type (class, array).

If a PHP file is dedicated to storing classes, the naming convention is

Class name. class.php

Memory storage:

Heap Area: Object

Stack area: basic data type

How to create an object:

$ object Name =new class name ();

Or

$ object Name =new class name;

How to access (use) the properties of an object:

Member properties are public before they can be accessed from the external

$ object Name-> property name;

Cases:

<?php
Class Cat {
Public $name; public $name; is a member variable
Public $age;
Public $color;
}
$cat 1 = new Cat ();
$cat 1->name = "small white";
$cat 1->age = 3;
$cat 1->color = "Yellow";
$cat 2 = new Cat ();
$cat 2->name = "Floret";
$cat 2->age = 10;
$cat 2->color = "white";
if ($cat 1->name = = "small white") {
echo "$cat 1->name". ' <br/> '. "$cat 1->age". ' <br/> '. "$cat 1->color";
}
?>

Description

1. If you pass an object to a function, it actually passes an address;

Cases:

<?php
Class Person {
Public $name;
Public $age;
}
$p 1 = new person ();
$p 1->name = "www.bianceng.cn";
$p 1->age = "10";
When the function passes in the object, the address is passed in
function Test1 ($p) {
$p->name = "Xiao Li";
}
Test1 ($p 1);
Echo $p 1->name; Output Xiao Li
?>

2. If the function is passed a basic data type (integer, Decimal, Boolean, String), by default, the value is passed. If you want to deliver an address, you can use the following form:

function test (& variable ...) {
}

Cases:

<?php
Passing basic data types to functions
$a = 90;
$b = 90.8;
$c = true;
$d = "www.bianceng.cn";
function test1 ($a, $b, $c, $d) {
$a = 78;
$b = 55.3;
$c = false;
$d = "good";
}
Test1 ($a, $b, $c, $d); Call function
echo $a. "||" . $b. "||" . $c. "||" . $d; Output 90| | 90.8| | 1| | www.bianceng.cn
?>

3. In PHP, if you pass an array to a function, the default is to pass the value. If you want to pass in an address, add the address character, as follows:

& Array Name

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.