1. Custom functions
<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 functions in the class
1. Double colon::, no need to instantiate, just call the class name directly
<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);