1 / 8

Създаване на клас

Създаване на клас. class име_на клас // Деклариране на класа { // Деклариране на поле ( променлива на класа ) var $име_на_променлива; // Деклариране на метода ( функция на класа ) function име_на_функция ( $аргумент 1, [...] [...], $аргумент n) { // Код на функцията

fedora
Download Presentation

Създаване на клас

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Създаване на клас class име_на клас // Деклариране на класа { // Деклариране на поле ( променлива на класа ) var $име_на_променлива; // Деклариране на метода ( функция на класа ) function име_на_функция ( $аргумент 1, [...] [...], $аргумент n) { // Код на функцията } // Тук свършва декларацията на метода } // Тук свършва декларацията на класа

  2. Езикът PHP използва специална променлива $this за осъществяванена достъп до даден клас. Използването на променливата $this в PHP е аналогично на създаването на инстанция на класа в самия клас. <?php class PrintName { var $name = "George"; function show_name() { echo "Hi $this -> name. } } $obj1 = new PrintName; $obj1 -> show_name(); ?>

  3. 1. <HTML><HEAD> <TITLE>Create a Class</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"> <?php class Person { function __construct($name){ $this->name=$name; } function getName(){ return $this->name; } protected $name; } $John = new Person("John"); $x= $John->getName(); echo $x; ?> </BODY></HTML>

  4. 2.<HTML><HEAD><TITLE>Create a class</TITLE><?phpclass Person{ function __construct($name,$egn){ $this->name=$name; $this->egn=$egn; } function getName(){ return $this->name; } function getEgn(){ return $this->egn; } protected $name; protected $egn;}$John = new Person("John","3212119988");echo "<BR>".$John->getName();echo "<BR>".$John->getEgn();?></BODY></HTML>

  5. 3. <HTML><HEAD><TITLE>Class_perant</TITLE></HEAD> <?php class Person { function __construct($name,$EGN){ $this->name=$name; $this->EGN=$EGN; } public function getEGN(){ return "<BR>name=".$this->name." EGN=".$this->EGN; } public function getName(){ return $this->name; } protected $name; protected $EGN; } class Student extends Person { function __construct($name,$EGN,$fnom){ $this->fnom=$fnom; parent::__construct($name,$EGN); } public function getFnom(){ return $this->fnom; } public function get_parent(){ //for error generation return parent; } protected $fnom; } $John = new Student("John","02124356","8512191105"); echo "<BR>Факултетен номер=".$John->getFnom(); echo $John->getEGN(); echo "<BR>име=".$John->getName(); ?> </BODY></HTML> Разширяване на клас - наследяване на класовеиме_на_класа extends име_на_родителския_клас

  6. 4. <HTML>Clas Static<HEAD> <TITLE></TITLE></HEAD><BODY > <?php class Person { function __construct($name){ $this->name=$name; //$this->age=$age; } public function getName(){ return $this->name; } public static function show_meaning(){ echo self::$meaning; } protected $name; protected $age; public static $meaning="Personal information"; } $John = new Person("John"); $Peter = new Person("Peter"); $x= $John->getName()."<BR>"; echo $x; $x= $Peter->getName()."<BR>"; echo $x; Person::show_meaning(); //Call of static class method ?> </BODY></HTML>

  7. <?php class Person { public $name="Peter"; public $clone_number=0; function __clone(){ $this->clone_number++; //increment number in clonned oject echo "<center>make clonning</center><HR>"; } } //end of the class $a = new Person(); $b = $a; //copy object $b->name="John"; echo "b name=".$b->name."<BR>"; $c = clone $a; //make clonning of $a $d = clone $c; //make clonning of $c echo "a name=".$a->name."<BR>"; echo "a clone_number=".$a->clone_number."<BR>"; echo "c name=".$c->name."<BR>"; echo "c clone_number=".$c->clone_number."<BR>"; echo "d name=".$d->name."<BR>"; echo "d clone_number=".$d->clone_number."<BR>"; ?>

  8. 5.<HTML><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><?phpclass Person{ public $name="Peter"; public $clone_number=0;// function __clone(){ $this->clone_number++; //increment number in clonned oject echo "<center>make clonning</center><HR>"; }} //end of the class$a = new Person();$b = $a; //copy object$b->name="John";echo "b name=".$b->name."<BR>";//$c = clone $a; //make clonning of $a$d = clone $c; //make clonning of $c echo "a name=".$a->name."<BR>"; echo "a clone_number=".$a->clone_number."<BR>"; echo "c name=".$c->name."<BR>"; echo "c clone_number=".$c->clone_number."<BR>"; echo "d name=".$d->name."<BR>"; echo "d clone_number=".$d->clone_number."<BR>"; ?></BODY></HTML>

More Related