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);
Output result:
add test:3
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);
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);
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.