Home  >  Article  >  Backend Development  >  PHP development introductory tutorial - object-oriented_PHP tutorial

PHP development introductory tutorial - object-oriented_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:59:20824browse

PHP是弱变量的脚本语言,也就是说你无需先定义,这一点很灵活。也可以给予很大的自由度,但对于程序来说,自由并非好事。

  因为给予后期维护人员阅读带来很大的麻烦。

  下面来进入我们第一个程序:

  1.php

////////////////////
<?class helloWorld{
 var $hellow_str = "Hello World!";
 function helloWorld(){
  echo $this->hellow_str;
 }
}
$p = new helloWorld;
?>
////////////////////

  很明显,这是一个类,很简单的类,只有一个构造函数。目的就是输出"Hello World";首先我们定义了一个类成员变量,通过一个特殊的$this对象,输出该变量,oop思想我不是很精,仅仅在于php开发方面用到,其他的也只是借鉴,但是我想对于php开发者已经足够了。

  接下来,解释下什么是$this对象,从这点我们先要知道什么是类。

  类,可以解释为一群相同的群体,在类中的成员变量,如上面的$hellow_str可以看作是这个群体的独特的特征,比如,桌子群体是一个
类,桌子都有桌面,桌脚,这些“都有”的东西,就是桌子全体的共有特征,在程序类里面我们就可以定义其为 桌子类的一个成员变量。

////////////
class desktop{
 var $desktop;#桌子面;
 var $baluster;#桌子腿;
}
///////////

  同样,在桌子世界里,他们也有共同的动作,比如移动[也许你现在就在推桌子^^],也可能我们要把桌子的面子放大点,一样,这个就是桌子群提的共有方法了,[和特征近似哦,不过这是动作罢了]。让我们把这个共同特征加到类中去吧。

////////////////
class desktop{
 var $desktop;#桌子面;
 var $baluster;#桌子腿;
 function move(){
  #....
 }
 function largen(){
  #...
 }
}///////////////

  知道了上面的,那么接下来就很好理解什么是$this对象了,没有错,他就是桌子群中的一个特定的对象,如果阁下还不能理解,那么,
我们接着解释。

  如果,我们现在要对一张桌子进行整改,比如,我们想把桌子面积做的更大,那么,我们针对的就不是一群桌子,而是这个要整改的特定的桌子,这是个个体!所以,我们要把这个类进行“实例化”进行精确定位。因为,其他桌子我们不需要整改。

////////////////
class desktop{
 var $desktop;#桌子面;
 var $baluster;#桌子腿;
 function move(){
  #....
 }
 function largen(){
  $this->desktop++;#放大桌子面
 }
}
$d = new desktop;#“实例化,这个时候我们就是针对一个特定的桌子拉!
$d->largen();#哈哈,把这个特定桌子面放大点。其实$d = $this;明白吗,$d就是那个特定的桌子,$d->largen就是用那个特定桌子[桌子群大家都有的变大方法]把特定桌子面放大了点。
///////////////


  原来,$this就是指一个特定桌子呀,哈哈,明白了,原来类是大家都有的属性,方法的集合,而一个特定的对象,就是这个群体中的一个个体,既然是他们当中的个体,当然拥有群体所拥有的共同特征和方法拉。

  成员变量,成员方法[成员函数],实例化,大家都知道了。

  但是我桌子虽然想变大,可是我还不知道桌子开始有多大啊,这可能吗,怎么办呢?

  接下来,我们隆重推出 构造函数 来拉。要说到构造函数啊,话多啊,就不多说了,他就是给我们桌子面和桌子腿确定大小的。

////////////////
class desktop{
 var $desktop;#桌子面;
 var $baluster;#桌子腿;
 function desktop(){
  $this->desktop = 100;
  $this->baluster = 100;
 }
 function move(){
  #....
 }
 function largen(){
  $this->desktop++;#放大桌子面
 }
}
////////////
看到了吧,我把桌子定义了大小和长度哦
//////////

  聪明的你,一定想立即实例化了吧,如果我开始在定义成员变量里就给值了,那不是说所有桌子都一样大吗?

//////////////
class desktop{
 var $desktop;#桌子面;
 var $baluster;#桌子腿;
 function desktop(){
  $this->desktop = 100;
  $this->baluster = 100;
 }
 function move(){
  #....
 }
 function largen(){
  $this->desktop++;#放大桌子面
 }
}
$d = new desktop;
$d->desktop();
////////////////////

But the result is: Call to a member function desktop() on a non-object in

Unlucky, the author must be cheating on clicks, bs is so small, TT.

In fact I was wrong, I know I was wrong. It turns out that the function we just added

function desktop(){
 $this->desktop = 100;
 $this->baluster = 100;
}

 It is a constructor. What is a constructor? Oops, it turns out that the constructor is a class-specific function. After the class is instantiated, the class will automatically execute the constructor in the first step, which is opening up a memory unit for the class.

In order to verify whether it is executed from the beginning, please focus on the first code in this tutorial. Wow, you see a method and a constructor with the same name as the class. We have already output the code after instantiation, which proves that I am not wrong yet.

Okay, back to our table world, you found that all the table tops and legs are 100 at this time.

2.php

Execution code

//////////////
<?
class desktop{
var $desktop;#Desktop;
var $baluster;#Table legs;
function desktop(){
$this->desktop = 100; $this->desktop."Size!<br />";
 }
 function move(){
 #....
 }
function largen(){
$this->desktop++;#Enlarge the desktop
echo "7~, I renovated the small desk today, so I won’t blame you, haha, I have more face than you now, I have ".$this->desktop." "Quack";
}
}
$d = new desktop;
$d->largen();
?>

Small table, escaped from the table family, Because our reputation is not as big as that of a small table. The old man at the table can't stand it any more. It's up to me to show you the demo.


//////////////
<?
class desktop{
var $desktop;#Desktop;
var $baluster;#Table legs;
function desktop($desktop){
$this->desktop = $desktop;
$this->baluster = 100;
echo "Our desk family orders everyone to give me the same face on the table In ".$this->desktop." size!
 $this->desktop++;#Enlarge the desktop
echo "7~, I renovated the small desk today, so I don’t mind you, haha, I have more face than you now, I have".$this-> desktop."Gaga";
}
}
$d = new desktop;
$d->largen();
?>

Small table, turn left I turned, turned right and looked around for a long time. I was tired of shopping and felt that I had evolved now and it was time to see the new kind. So after the table transformation was executed, I found...


/////
$d = new desktop(101);
////

 My tmd is still a small table.


http://www.bkjia.com/PHPjc/317374.html
www.bkjia.com
true

http: //www.bkjia.com/PHPjc/317374.htmlTechArticlePHP is a weak variable scripting language, which means you don’t need to define it first, which is very flexible. A lot of freedom can also be given, but freedom is not a good thing for a program. Because after giving...
Statement:
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