PHP array {array} use Tutorial

Source: Internet
Author: User
Tags php and

PHP array {array} use Tutorial

Now, this lengthy setting uses a set of methods for each attribute-there must be a simpler way. This is the associative array that comes in handy with our constructs. Let's create our user object and give it a few instances when performance

$attribs = Array (
' Name ' => ' Kirupa ',
' Job ' => ' Engineer ',
' School ' => ' MIT ',
' Hometown ' => ' Spanish Fort ',
' Homestate ' => ' Alabama '
);
$kirupa = new User ($attribs);

Echo ' Name: '. $kirupa->getname (). '  <br/> '; Kirupa
Echo ' Job: '. $kirupa->getjob (). '  <br/> '; Engineer
Echo ' School: '. $kirupa->getschool (). '  <br/> '; MIT
Echo ' Home Town: '. $kirupa->gethometown (). ' <br/> '; Spanish Fort
Echo ' home state: '. $kirupa->gethomestate (). ' <br/> '; Alabama

Class User {
Private $name;
Private $job;
Private $school;
Private $homeTown;
Private $homeState;

function __construct ($attribs) {
$this->name = $attribs [' name '];
$this->job = $attribs [' Job '];
$this->school = $attribs [' School '];
$this->hometown = $attribs [' Hometown '];
$this->homestate = $attribs [' homestate '];
}

/* Name Methods * *
function SetName ($val) {
$this->name = $val;
Return
}

function GetName () {
return $this->name;
}

/* Job Methods * *
function Setjob ($val) {
$this->job = $val;
Return
}

function Getjob () {
return $this->job;
}

/* School Methods * *
function Setschool ($val) {
$this->school = $val;
Return
}

function Getschool () {
return $this->school;
}

/* Home town methods * *
function Sethometown ($val) {
$this->hometown = $val;
}

function Gethometown () {
return $this->hometown;
}

/* Home state methods * *
function Sethomestate ($val) {
$this->homestate = $val;
}

function Gethomestate () {
return $this->homestate;
}
}

I have been a set of methods if we want to change the properties, but now the performance is established via __construct front. That is, we pass our $ attribs An instance of an array object, which in turn goes through the automatic construction of the $ attribs. Advantages using associative arrays for this purpose, we do not need to specify all the variables and methods explicitly. It is extremely convenient when the data will be with MySQL PHP and we will pay as soon as possible. But first, we need to learn a little bit about the interaction of methods and the grouping of objects.

Law interaction
At this point, our approach does not interact. Let's add a small feature, for example, to our full state conversion name to the appropriate abbreviation.

$attribs = Array (
' Name ' => ' Kirupa ',
' Homestate ' => ' Alabama '
);
$kirupa = new User ($attribs);

Echo ' Name: '. $kirupa->getname (). '   <br/> '; Kirupa
Echo ' home state: '. $kirupa->gethomestate (). '  <br/> '; Alabama
Echo ' state Abbr.: '. $kirupa->gethomestateabbr (). ' <br/> '; AL

Class User {
Private $name;
Private $homeState;

function __construct ($attribs) {
$this->name = $attribs [' name '];
$this->homestate = $attribs [' homestate '];
}

/* Name Methods * *
function SetName ($val) {
$this->name = $val;
Return
}

function GetName () {
return $this->name;
}

/* Home state methods * *
function Sethomestate ($val) {
$this->homestate = $val;
}

function Gethomestate () {
return $this->homestate;
}

function Gethomestateabbr () {
$ABBR = States:: $ABBRS [$this->gethomestate ()];
if ($ABBR) {
return $ABBR;
}
else {
Return ' Unknown ';
}
}
}

Class States {
/* Ideally this would is held in a database */
Static $ABBRS = Array (
' Alabama ' => ' AL ',
' Michigan ' => ' MI ',
' New York ' => ' NY '
);
}

In this example, we make full use of the name of the country to obtain the corresponding abbreviation. We do this one class using the same static variable. Static variables generally do not change, but there is no way. The behavior of these variables is more reference and can be used for a wide range of variables like your database connection settings and other sites. Static variables are usually named with all uppercase letters.

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.