Can the function parameters in a class be specified as class attributes?

Source: Internet
Author: User
Can I specify the parameter of the function in the class as the property of the class? see a code saying that one of the parameters of the USB function in the class can be the above USB interface as the parameter ,, I have never heard of the use of parameters in PHP functions. why?


Reply to discussion (solution)

Function useUSB (USB $ usb)
This USB is a type declaration. if $ usb is not a USB Type, a syntax error will occur.

Function useUSB (USB $ usb)
This USB is a type declaration. if $ usb is not a USB Type, a syntax error will occur.


In this case, I understand the function parameters. in my understanding, the function parameters are a variable, which can be any value without type restrictions.

If your understanding is overturned, it only means that your understanding is wrong.
Therefore, it is not a disruption, but an enhanced understanding.
For example, the USB class has a load method, and you
Function useUSB ($ usb ){
$ Usb-> load ();
}
This definition calls $ obj-> useUSB ('ABC ');
This will cause an error where the load method does not exist at $ usb-> load ().
So you need to check whether $ usb is an object before $ usb-> load (); whether the load method exists
After the type declaration is added, this check is completed by the php parser.

You still didn't understand what I said. I mean that the first parameter USB in the useUSB function is interface-type USB. What I don't understand is here, how can a function parameter be a class? As I have seen in many tutorials, the parameters in the function are a measurable parameter. the number of parameters you define is what you call. but what is the problem with this class, in many tutorials, the medium pressure root is not mentioned. That's exactly what it feels like. What I don't understand is here.

You didn't understand what I mean!
Function parameters are separated by commas (,).
Is there a comma between USB $ usb?
Obviously, USB $ usb is a parameter.

You still didn't understand what I said. I mean that the first parameter USB in the useUSB function is interface-type USB. What I don't understand is here, how can a function parameter be a class? As I have seen in many tutorials, the parameters in the function are a measurable parameter. the number of parameters you define is what you call. but what is the problem with this class, in many tutorials, the medium pressure root is not mentioned. That's exactly what it feels like. What I don't understand is here.



Function use (USB $ usb ){}
$ Usb is an object pointer instantiated by the USB class
For example, $ usb1 = USB (); $ usb2 = USB (); use ($ usb1); use ($ usb2 );

I am a little enlightened. I am a little enlightened by the two answers. I don't know if I understand this. no, the () in the function is an expression. This expression can be used to declare classes, for example, in my example, USB $ usb is equivalent to $ usb = new USB.

In this example, USB $ usb
Indicates that $ usb must be a USB-like instance or an instance of a USB-like class.

In this example, USB $ usb
Indicates that $ usb must be a USB-like instance or an instance of a USB-like class.


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

No. it must be an instance.

Function use (USB $ usb ){}
Equivalent
Function use ($ usb ){
If (! Is_a ($ usb, 'USB ') return 'error ';
}

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.