国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP 5.0對象模型深度探索之屬性和方法

PHP 5.0對象模型深度探索之屬性和方法

Jun 21, 2016 am 09:11 AM
name public

對象

??? 一個對象實例的屬性是變量,就像PHP的其他變量一樣。但是你必須使用->運算符來引用它們。不需要在屬性前使用美元符$。

  可以聯(lián)用->,如果一個對象的屬性包含了一個對象,你可以使用兩個->運算符來得到內(nèi)部對象的屬性. 你甚至可以用雙重引用的字符串來放置這些表達(dá)式. 下面的例子中,對象House中的屬性room包含了一組Room對象。

  訪問方法和訪問屬性類似。->運算符用來指向?qū)嵗姆椒? 在下面的中調(diào)用getLastLogin就是。方法執(zhí)行起來和類外的函數(shù)幾乎相同.

  如果一個類從另一類中繼承而來,父類中的屬性和方法將在子類中都有效,即使在子類中沒有聲明. 像以前提到過的,繼承是非常強大的. 如果你想訪問一個繼承的屬性,你只需要像訪問基類自己的屬性那樣引用即可,使用::運算符.

class Room
{
 public $name;

 function __construct($name="unnamed")
 {
  $this->name = $name;
 }
}

class House
{
 //array of rooms
 public $room;
}

//create empty house
$home = new house;

//add some rooms
$home->room[] = new Room("bedroom");
$home->room[] = new Room("kitchen");
$home->room[] = new Room("bathroom");

//show the first room of the house
print($home->room[0]->name);
?>
  PHP有兩個特殊的命名空間:parent命名空間指向父類,self命名空間指向當(dāng)前的類。下面的例子中顯示了如何用parent命名空間來調(diào)用父類中的構(gòu)造函數(shù). 同時也用self來在構(gòu)造函數(shù)中調(diào)用另一個類方法。

class Animal //動物
{
 public $blood; //熱血or冷血屬性
 public $name;
 public function __construct($blood, $name=NULL)
 {
  $this->blood = $blood;
  if($name)
  {
   $this->name = $name;
  }
 }
}

class Mammal extends Animal //哺乳動物
{
 public $furColor; //皮毛顏色
 public $legs;

 function __construct($furColor, $legs, $name=NULL)
 {
  parent::__construct("warm", $name);
  $this->furColor = $furColor;
  $this->legs = $legs;
 }
}

class Dog extends Mammal
{
 function __construct($furColor, $name)
 {
  parent::__construct($furColor, 4, $name);

  self::bark();
 }

 function bark()
 {
  print("$this->name says 'woof!'");
 }
}

$d = new Dog("Black and Tan", "Angus");
?>
  對于對象的成員來是這樣調(diào)用的:如果你需要在運行時確定變量的名稱,你可以用$this->$Property這樣的表達(dá)式。 如果你想調(diào)用方法,可以用$obj->$method()。

  你也可以用->運算符來返回一個函數(shù)的值,這在PHP以前的版本中是不允許的。例如,你可以寫一個像這樣的表達(dá)式: $obj->getObject()->callMethod()。這樣避免了使用一個中間變量,也有助于實現(xiàn)某些設(shè)計模式,如Factory模式。



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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What is the difference between the developer version and the public version of iOS? What is the difference between the developer version and the public version of iOS? Mar 01, 2024 pm 12:55 PM

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Java function access permission modifier public usage guide Java function access permission modifier public usage guide Apr 26, 2024 am 08:39 AM

The Java public access modifier allows functions to be accessed from anywhere and is used to declare public APIs and define tools and utilities that are shared across packages or classes. The specific usage is as follows: Syntax: public return value type function name (parameter list) {...} Scenario: functions that need to be accessed from anywhere, methods in public APIs, shared tools or utilities

What should I do if php cannot get the name? What should I do if php cannot get the name? Nov 24, 2022 am 09:56 AM

PHP cannot get the name because when the name and id values ??of the form element are different, the browser cannot recognize it. The solution: 1. Check whether some form elements and frame elements use name; 2. Check only Elements that can be assigned ID but not name; 3. For multi-select box checkbox, you can use "join(',', $__POST['name'])" to form data.

How to add name to setup in Vue3 How to add name to setup in Vue3 May 13, 2023 am 09:40 AM

What is the use of name in Vue3? 1. Name needs to be defined when making recursive components. 2. The component can be cached with keep-aliveincludeexclude. 3. When Vue reports an error or is debugging, you can see the name of the component. Vue3 defines name1. It is automatically generated as long as the setup syntax sugar mode single file component is turned on in the script. The corresponding name option will be automatically generated based on the file name. For example, Tree.vue, then its name will be automatically generated by Tree. This has a drawback. If you want to modify the name, you need to modify the component name. If there is a place to import the component, you need to modify it together. 2. Open a script to define name

Java programs display different access levels Java programs display different access levels Aug 19, 2023 pm 10:09 PM

Access modifiers are used to set the feature of visibility of some particular classes, interfaces, variables, methods, constructors, data members, and the setter methods in Java programming language. In Java environment, we have different types of access modifiers. Default - If we declare a function, it will only be visible in a specific package. Private- If we declare a function, it will be available only in a specific class

How to use the public modifier in java How to use the public modifier in java Apr 18, 2023 pm 06:04 PM

1. Any other class can access classes, methods, constructors and interfaces declared as public. 2. If the public classes that access each other are distributed in different packages, you need to import the package where the corresponding public class is located. Due to class inheritance, all public methods and variables can be inherited by its subclasses. Example publicclassdemo1{publicstaticvoidmain(String[]args){Personp1=newPerson();p1.fn();System.out.println(p1.a);//100System.out.println(p1.scorce);/

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒問題。

See all articles