Home > Backend Development > PHP Tutorial > PHP learning diary (1) - the use of classes and functions

PHP learning diary (1) - the use of classes and functions

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:13:55
Original
1043 people have browsed it

1. Custom function

<span>function</span> add(<span>$a</span>,<span>$b</span><span>){
    </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
    </span><span>echo</span> 'add test:'<span>;
    </span><span>echo</span><span>$c</span><span>;
    </span><span>return</span><span>$c</span><span>;
}
add(</span>1,2);
Copy after login

Output result:

add test:3
Copy after login

2. Call the function in the class

1. Double colon::, no need to instantiate, direct class name call

<span>class</span><span> test{
    </span><span>public</span><span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span><span>$c</span><span>;        
        </span><span>return</span><span>$c</span><span>;
    }
}
test</span>::add(1,2);
Copy after login

2. - >, the instantiated object uses

<span>class</span><span> test{
    </span><span>public</span><span>function</span> add(<span>$a</span>,<span>$b</span><span>){
        </span><span>$c</span>=<span>$a</span>+<span>$b</span><span>;
        </span><span>echo</span> 'class test:'<span>;    
        </span><span>echo</span><span>$c</span><span>;        
        </span><span>return</span><span>$c</span><span>;
    }
}
</span><span>$object</span>=<span>new</span><span> test();
</span><span>$object</span>->add(1,3);
Copy after login

The above introduces the PHP learning diary (1) - the use of classes and functions, including objects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
Latest Issues
test
From 1970-01-01 08:00:00
0
0
0
What does: self/Test mean after the function?
From 1970-01-01 08:00:00
0
0
0
File permission setting test failed
From 1970-01-01 08:00:00
0
0
0
How to stop function execution under test?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template