Php Getting Started Tutorial (class instance tutorial)

Source: Internet
Author: User
Tags php class php tutorial

This section describes how to call the php Tutorial class, apply for instructions, and use the previous instance methods and operation procedures of various types, let you know the construct destruct clone call wake sleep usage of the php class clearly.

Simple shopping cart

<? Php
Class cart
{
Var $ items; // items in the shopping cart
// Put $ num $ artnr in the car
Function add_item ($ artnr, $ num)
    {
$ This-> items [$ artnr] + = $ num;
    }
// Extract $ num $ artnr from the vehicle
Function remove_item ($ artnr, $ num)
    {
If ($ this-> items [$ artnr]> $ num ){
$ This-> items [$ artnr]-= $ num;
Return true;
} Else {
Return false;
        }
    }
}
?>

Class with constructor

<? Php
Class session
{
Public function _ construct ()
{
Echo 'constructors execute ';
    }
Public function _ destruct ()
    {
Echo 'destructor running ';
    }
}
$ Obj = new session;
Unset ($ obj); // delete an object, _ destruct () will be called
// Output
// Constructor execution
/Destructor execution
?>

Class Call and clone

<? Php
Class session
{
Public $ age = 20;
Public $ sub = null;
Public function _ clone ()
 {
$ This-> sub = clone $ this-> sub;
 }
}
Class session 2
{
Public $ value = 5;
}
$ S = new session;
$ S-> sub = new session 2;
$ S2 = clone $ s;
$ S-> sub-> $ value = 10;
Echo $ s2-> sub-> $ value;
?>

Class call method

<? Php
Class session
{
Protected function _ call ($ func, $ para)
 {
Echo 'method does not exist: 'func. "n". 'parameter:'. "n ";
Print_r ($ para );
 }
}
$ S = new session ();
Echo $ c-> getnames ('hello', 'you ');
/*
Output:
Method does not exist: getnames
Parameters:
Array
(
[0] => hello
[1] => you
)
*/
?>

Wake sleep

<? Php
// Define a session class
Class session {
Public $ sessionvars;
Public $ id;
Function session (){
$ This-> id = uniqid ();
 }
Function _ sleep (){
 }
Function _ wake (){
$ This-> id = uniqid ();
 }
}
// Create an object
$ S = new session;
$ S-> sessionvars = "1.02 ";

// Serialize
$ Ss = serialize ($ s );
// Unserialize it deserialization id is re-assigned
$ S2 = unserialize ($ s );
// $ S and $ s2 have different IDs
Print_r ($ s );
Print_r ($ s2 );
?>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.