Can parameters of a function in a class be specified as a property of a class?

Source: Internet
Author: User
See a code, said to be a class of usbusb function of one of the parameters can be the above USB interface as a parameter, and have not heard the PHP function parameters so use, what is the reason?


Reply to discussion (solution)

function Useusb (USB $usb)
This USB is a type declaration, if the $USB is not a USB type, there will be a syntax error

function Useusb (USB $usb)
This USB is a type declaration, if the $USB is not a USB type, there will be a syntax error


If so, it really upsets my understanding of the function parameters, which I understand is that the function parameter is a variable, can be any value, and there is no type restriction.

If it's a reversal of your understanding, it only means that your understanding is problematic.
So it's not subversive, it's an enhanced understanding.
For example, a USB class has a load method, and you
function Useusb ($USB) {
$usb->load ();
}
This is defined so that the call $obj->useusb (' abc ');
This will cause an error that does not exist in the Load method at the $usb->load ()
Then you need to check whether the $USB is an object before $usb->load (), or if there is a Load method
And with the type declaration, this check is done by the PHP parser.

You still don't understand what I'm talking about, I mean the function useusb the first parameter in the function USB is the interface class USB, I do not understand is here, how the parameters of the function will be a class? What I've seen in many tutorials is that the parameters in the function are a quantity, what you define, a call comes out, but this class, what is going on, is not mentioned in many tutorials. It's extrapolate, it feels incredible. What I don't understand is here.

You don't understand what I mean!
The arguments to the function are separated by commas
and is there a comma between the USB $usb?
Obviously not, then the USB $usb is a parameter

You still don't understand what I'm talking about, I mean the function useusb the first parameter in the function USB is the interface class USB, I do not understand is here, how the parameters of the function will be a class? What I've seen in many tutorials is that the parameters in the function are a quantity, what you define, a call comes out, but this class, what is going on, is not mentioned in many tutorials. It's extrapolate, it feels incredible. What I don't understand is here.



function use (USB $usb) {}
$USB is a pointer to an object instantiated by the USB class
For example $usb 1 = USB (); $usb 2 = USB (); Use ($usb 1); Use ($usb 2);

A little enlightened, through the two-bit solution, is a bit of understanding, I do not know that I do not understand, the function is an expression in the (), the expression can be used to declare the method of the class, such as my example, USB $usb, is not equivalent to $usb=new USB

In this example, the USB $usb
Indicates that the $USB must be an instance of class USB or a class that inherits from USB

In this example, the USB $usb
Indicates that the $USB must be an instance of class USB or a class that inherits from USB


Can $USB be a USB property or method???

No, you must be an instance

function use (USB $usb) {}
Equivalent
function Use ($USB) {
if (! is_a ($USB, ' USB ')) return ' error ';
}

  • Related Article

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.