Table of Contents
回复讨论(解决方案)

成员变量问题

Jun 23, 2016 pm 01:54 PM
variable member

最近自学php 看到一段代码 
class queryFactory extends base {
  var $link, $count_queries, $total_query_time;

queryFactory 这个类里面 只有$link, $count_queries, $total_query_time这几个变量。
但是queryFactory 里面有个connect方法
    function connect($zf_host, $zf_user, $zf_password, $zf_database, $zf_pconnect = 'false', $zp_real = false) {
    $this->database = $zf_database;
    $this->user = $zf_user;
    $this->host = $zf_host;
    $this->password = $zf_password;
    $this->pConnect = $zf_pconnect;
    $this->real = $zp_real;

这里面出现了 $this->database ,我没有在queryFactory 和queryFactory 的父类里面找到database这个成员的定义。
但在外面的确能访问到database 这个变量。

请问作者为什么不把database像  var $link, $count_queries, $total_query_time这些变量一样定义在一起。这样使用到底有什么好处和意义啊


回复讨论(解决方案)

function connect($zf_host, $zf_user, $zf_password, $zf_database, $zf_pconnect = 'false', $zp_real = false) {    $this->database = $zf_database;    $this->user = $zf_user;    $this->host = $zf_host;    $this->password = $zf_password;    $this->pConnect = $zf_pconnect;    $this->real = $zp_real;
Copy after login

这是 php4 的编程风格
一般的说,显式的声明在类属性列表的属性,是为了表示这些属性一定存在并可被使用

至于在 connect 方法中将传入的参数赋值给类属性是不可取的

这是 php4 的编程风格
一般的说,显式的声明在类属性列表的属性,是为了表示这些属性一定存在并可被使用

至于在 connect 方法中将传入的参数赋值给类属性是不可取的



$this->database 我可以认为database像var $link, $count_queries, $total_query_time;是这个类的属性吗 

按先?明,後使用的原??好,可以?少不少bug。

这是PHP定义变量的一种形式,和直接在方法外定义是一样的,只要执行了这个connect方法,就能取到;
他不和其他变量一起定义,是想用到的时候再定义,这样就避免定义了变量但有可能不用从而占用内存的情况,这是我觉得最合理的地方。

$this->database ? $link $count_queries 一?,都是???的?性。
都可以?外面使用。

$this->database 我可以认为database像var $link, $count_queries, $total_query_time;是这个类的属性吗 


是的,不是像,而是就是这个类的属性
从调用方式看,你只要调用 connect 方法,就必须传递 $zf_database 参数。那么把它作为类属性保存起来的意义何在呢?
传递给 connect 的参数都是一次性参数,用完就不要了,所以并不需要保存。而且在其他地方也不会被用到

这也是PHP的优势吧,变量不用事先声明

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

PHP function introduction—is_string(): Check whether the variable is a string PHP function introduction—is_string(): Check whether the variable is a string Jul 24, 2023 pm 09:33 PM

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Explain the life cycle of variables in C language Explain the life cycle of variables in C language Sep 02, 2023 pm 07:37 PM

Storage classes specify the scope, lifetime, and binding of variables. To fully define a variable, it is necessary to mention not only its "type" but also its storage class. A variable name identifies a physical location in computer memory where a set of bits is allocated to store the variable's value. Storage class tells us the following factors - Where are the variables stored (in memory or CPU registers)? If not initialized, what is the initial value of the variable? What is the scope of a variable (the scope within which the variable can be accessed)? What is the life cycle of a variable? The lifetime of a lifetime variable defines the duration for which the computer allocates memory (the duration between memory allocation and deallocation). In C language, variables can have automatic, static or dynamic life cycle. Automatic - create a life cycle with automatic

See all articles