PHP Object-Oriented basics

Source: Internet
Author: User
Tags php oop php class

<?php
PHP OOP Learning
Class person{

Public $name;
Public $age;
Public $height;

function getinstance ($name, $age, $height) {
$obj _person = new Person ();
$obj _person->name = $name;
$obj _person->age = $age;
$obj _person->height = $height;
return $obj _person;
}
}

$obj _person = new Person ();

$class _data = $obj _person->getinstance ("Bin Chen", 100, 171);

Var_dump ($class _data);
?>

The PHP class name specification is typically a class name. class.php

Person.class.php

Memory Analysis:
Heap memory: something that puts objects

Stack memory: Put the base data type (if the base data type belongs to an object, it should also be placed in heap memory)

function is opened, new stack memory function call is completed to reclaim memory immediately

Static Memory: Static


Constant Memory: constant

Code Memory: Code directives


---------------------------
===========================
---------------------------


About memory partitioning, heap memory, stack memory, constant memory, static data memory, code memory
Question: Call function incoming object and base data type in the end is the memory address, or the value

$obj = new Person ();
function For_obj ($obj) {
$obj->name = "Chenbin";
}
For_obj ($obj);
Var_dump ($obj);

When the object is passed in, the memory address of the object is passed in, so we find the space by the address and modify the value.
So the object actually passes the memory address of the object.
$bin _a = 1;


function For_comm ($bin _a) {
$bin _a = 3;
Print_r ("= = =");
}
For_comm ($bin _a);
Var_dump ($bin _a);
When the underlying data type passed in is actually a value, because we cannot find space to modify the value.

$arr _a = Array (1,2,3,4);
Function For_arr (& $arr _a) {
$arr _a[3] = 100;
}
function for_arr_1 ($arr _a) {
$arr _a[3] = 100;
}
For_arr_1 ($arr _a);
Var_dump ($arr _a);

For_arr ($arr _a);
Var_dump ($arr _a);

Array pass-through parameter, by default it is a value passed instead of an address


Attention
*/
In a class, in a function to refer to a variable outside the function, you need to use $THIS->XX instead of the $ variable name
Class pig{
Public $weight;

function Weightadd ($num) {
$weight = $weight + $num;
$this->weight = $this->weight+ $num;
}
}

$pig = new Pig ();
$pig->weightadd (4);
Var_dump ($pig->weight);


?>

====================================
------------------------------------
====================================
PHP Construction Methods

Class mobile{

Public Function __construct () {
Echo $this->color;
}
This means that the constructor is called after the object is created.
Public $color = "BLACK";


PHP5, here's how it's written.
Public Function Mobile () {
Echo $this->color;
echo "-----";
}
}

$mobile = new mobile ();

PHP destructor
Definition: Executed when all references to an object are deleted or objects are explicitly destroyed

Primarily used to release resources such as closing database links


The order in which the destructor is called is destroyed after the object is first created

Stack is advanced after out

When a destructor is called:

A when the program exits

B when the object becomes a garbage object, the object immediately calls the destructor of the object

PHP destructor

Class ball{
Public $name;
Public function __construct ($name) {
$this->name = $name;
}
Public Function __destruct () {
echo $this->name. " Delete in \ n ";
}
}

$c 1 = new ball ("111");
$c 1 = null;//a when the program exits B when the object becomes a garbage object, the object immediately calls the destructor
$c 2 = new ball ("222");
$c 3 = new ball ("333");

The order in which destructors are executed is reversed and the reference to the object in the new is in the stack memory, because the stack is first in and out

PHP Static variables

*/
A static variable is used in a class with self:: The variable name does not work with the $this-> variable name, because the static variable belongs to the class rather than the object, so it is inappropriate to invoke this with this current object
/* Class cat{

Publicstatic $a = 0;

Public function __construct ($num) {
Self:: $a + = $num;
}
}
$a _1 = new Cat (12);
$a _2 = new Cat (12);
Var_dump ($a _2:: $a); */

PHP static method
Static methods

Class static_study{
Public $a = 4;
public static $b = 5;
private static $c = 6;
protected static $d = 7;

public static function test () {
Echo $this->a; Non-static member variables cannot be referenced in a static method
echo Self:: $b. " \ r ";
}

Public Function __construct () {
Static_study::test ();
Self::test ();
Self:: variable that can get any modifier inside the class
Static_study:: variable that can get any modifier inside the class
}
}

$study _study = new Static_study ();
$study _study->test ();
$study _study::test ();
Static_study::test ();
$study _study-> cannot get static variables and data for private and protected
$study _study:: Unable to get static variables and data for private and protected
Static_study:: Unable to get static variables and data for private and protected
Class outside of the call class internal static variables and static functions are available in the above three ways


Var_dump ($this, $a); This isn't working.

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.