.


:




:

































 

 

 

 


.

: PHP5 13, . 296-323

 

:

  1. .
  2. (public, private, protected)
  3. .

.

- () .

, , .

(), (), .

. PHP4, PHP5.

1

<?php

class myPHP4Class

{

var $my_variable;

function my_method($param)

{

echo " my_method($param)!\n";

echo " : ";

echo "{$this->my_variable}\n";

}

}

?>

 

$this , . .

 

() .

new.

2

<?php

include_once (myPHP4Class_def.php)

$myinstance = new myPHP4Class();

$anotherinstance = new myPHP4Class();

?>

$myinstance $anotherinstance myPHP4Class. .

->

3

<?php

include_once (myPHP4Class_def.php)

$myinstance = new myPHP4Class();

$anotherinstance = new myPHP4class();

$myinstance->my_variable = 10;

$anotherinstance->my_variable = 20;

$myinstance->my_method("MyParam");

?>

(public, private, protected)

 

PHP4 , .. public.

PHP5 3 : public, private, protected.

PHP5 1 : ( )

 

4

<?php

class myPHP5Class

{

public $my_variable;

public function my_method($param)

{

echo " my_method($param)!\n";

echo " : ";

echo "{$this->my_variable}\n";

}

}

?>

public () . ($this) ( -> ).

private () () $this.

4, private.

5

<?php

class myPHP5Class

{

private $my_variable;

public function my_method($param)

{

echo " my_method($param)!\n";

echo " : ";

echo "{$this->my_variable}\n";

}

}

$myobject = new myPHP5Class();

$myobject-> my_method(MyParam); //

$myobject->my_variable = 10; //

?>

, () (). : .

PHP4 .

PHP5 : __construct(), : __destruct().

6

<?php

class SimpleClass

{

function __construct($param)

{

echo " SimpleClass!";

}

function __destruct()

{

echo " SimpleClass";

}

}

 

$myinstance = new SimpleClass("value");

unset($myinstance); //

?>

, , .

PHP5. . const, .

7

 

<?php

class ConstExample

{

private $myvar;

public $readme;

const MY_CONSTANT = 10;

 

public function showConstant()

{

echo ": ".MY_CONSTANT;// : $this (.. )

}

}

 

$inst = new ConstExample;

$inst->showConstant();

echo ": ".ConstExample::MY_CONSTANT;

?>

 

, .

.

. (: < >::<>).

 

.

(static) , , . . $this, .

static.

static function myMetod()

{

 

}

:

< >::<>

, .

 

 

() ().

, , -. , public protected. , extends.

8 ( )

<?php

class ParentClass

{

public $parentvar;

 

public function parentOne()

{

echo "Called parentOne()\n";

}

 

private function parentTwo()

{

echo "Called parentTwo()!\n";

}

}

 

class ChildClass extends ParentClass

{

public function childOne()

{

echo "Called childOne()!\n";

}

/* parentOne() ,

ParentClass

parentTwo() */

}

 

$v = new ChildClass();

$v->parentOne();

?>

, protected -.

private .

public , - .

 

:

 

9 ( )

<?php

Class ParentClass

{

public function callMe()

{

echo " !\n";

}

}

 

class ChildClass extends ParentClass

{

public function callMe()

{

echo " !\n";

}

}

 

$child = new ChildClass;

$child-> callMe();

?>

ChildClass. .

 

10 ( PHP)

<?php

Class ParentClass

{

public function callMe()

{

$this->anotherCall();

}

 

public function anotherCall()

{

echo " !\n";

}

}

 

class ChildClass extends ParentClass

{

public function anotherCall()

{

echo " !\n";

}

}

 

$child = new ChildClass;

$child-> callMe();

?>

callMe() ChildClass, , ParentClass. anotherCall() $this. ? . . , $this , ( ChildClass), , $this.

 

 

.

PHP4

<?php

$class_one = new MyClass();

$class_one_copy = $class_one; // PHP5 $class_one_copy $class_one // $class_one $class_one_copy .

 

?>

PHP5

<?php

$class_one = new MyClass();

$class_one_copy = clone $class_one; // clone

//$class_one_copy $class_one $class_one //$class_one_copy

?>

PHP5 PHP4:



<== | ==>
| : , ,
:


: 2016-11-23; !; : 202 |


:

:

- - , .
==> ...

1869 - | 1821 -


© 2015-2024 lektsii.org - -

: 0.038 .