


In PHP, why can't we call the server method directly instead of just adding intermediate variables? This is a common question. PHP editor Apple will answer it for you. In PHP, we usually use front-end pages to interact with back-end servers. Directly calling server methods may involve some security risks, such as being used by hackers to perform malicious operations. To increase security, we introduce the concept of intermediate variables. By sending the front-end request to the server, and the server then performs corresponding operations based on the value of the intermediate variable, potential security risks can be effectively reduced. In addition, using intermediate variables can also increase the maintainability and scalability of the code, making the code clearer and easier to understand. In summary, adding intermediate variables is a safe and reliable practice that can improve the quality and reliability of your code.
Question content
I am writing a client for mqtt. I wrote a callback for mqtt.OnConnectHandler. I want to write the connection address of the print server.
var connectHandler mqtt.OnConnectHandler = func(c mqtt.Client) { // this is ok options := c.OptionsReader() s := options.Servers() fmt.Printf("conn: %s success\n", s) // -------------------------------------- // But it won't work like this // here have error: On servers。 // cannot call pointer method Servers on mqtt.ClientOptionsReadercompilerInvalidMethodExpr o := c.OptionsReader().Servers() fmt.Printf("conn: %s success\n", o) }
So, I found that I can't use it directly. Why? Aren't they the same thing? Why can only intermediate variables be used to receive?
I tried two methods. You only succeed once. I thought both would be fine.
Solution
This is because the Servers
method is declared with a pointer receiver. This means that in order to call Servers
, the receiver must be addressable:
https://www.php.cn/link/753a043674f0193523abc1bbce678686
Temporary variables are not addressable. here:
c.OptionsReader().Servers()
c.OptionsReader()
returns an unaddressable value, so you cannot call Servers
with that value as the receiver. When you assign the return value of c.OptionsReader
to a variable, it becomes addressable, and you can call Servers()
with that variable.
The above is the detailed content of Why can't I call the server method directly instead of just adding intermediate variables?. For more information, please follow other related articles on the PHP Chinese website!

The article explains how to use the pprof tool for analyzing Go performance, including enabling profiling, collecting data, and identifying common bottlenecks like CPU and memory issues.Character count: 159

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

The article discusses Go's reflect package, used for runtime manipulation of code, beneficial for serialization, generic programming, and more. It warns of performance costs like slower execution and higher memory use, advising judicious use and best

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization

The article discusses using table-driven tests in Go, a method that uses a table of test cases to test functions with multiple inputs and outcomes. It highlights benefits like improved readability, reduced duplication, scalability, consistency, and a

The article discusses managing Go module dependencies via go.mod, covering specification, updates, and conflict resolution. It emphasizes best practices like semantic versioning and regular updates.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
