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

WBOY
WBOYOriginal
2016-07-29 09:13:551034browse

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.

Statement:
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