Sunteți pe pagina 1din 5

Clase si obiecte

O clasa este o colectie de variabile si functii (metode).


Definire :
<?php
class Cart {
var $items; // Items in our shopping cart

// Add $num articles of $artnr to the cart

function add_item($artnr, $num {
$this!"items#$artnr$ %& $num;
'

// (a)e $num articles of $artnr out of the cart

function remove_item($artnr, $num {
if ($this!"items#$artnr$ " $num {
$this!"items#$artnr$ !& $num;
return true;
' else {
return false;
'
'
'
?"
Aceasta defineste o clasa numita Cart care contine un vector asociat articolelor din cos, si
doua functii care adauga sau sterg produse din cosul de cumparaturi.
ATENTIE: O clasa nu poate fi Sparta in mai multe blocuri PHP.
n PHP!, doar initiali"arile de variabile constante sunt permise cu a#utorul cuvantului
c$eie var.
Pentru initiali"area variabilelor cu valori ce nu sunt constante, aveti nevoie de o functie
de initiali"are care este apelata ori de cate ori este construit un obiect din acea clasa.
Acea functie se numeste constructor.
%&p$p
class Cart '
() *ici una din variabile nu va functiona. )(
var +toda,s-date . date(/01m1d/)2
var +name . +firstname2
var +o3ner . 45red 4 . 46ones42
() Arra,s containing constant values 3ill, t$oug$. )(
var +items . arra,(/7C8/, /97/)2
:
() ata cum trebuie scris. )(
class Cart '
var +toda,s-date2
var +name2
var +o3ner2
var +items . arra,(/7C8/, /97/)2
function Cart() '
+t$is1;toda,s-date . date(/01m1d/)2
+t$is1;name . +<=O>A=S?4firstname4@2
() etc. . . )(
:
:
&;
Pentru creerea unui obiect se foloseste operatorul ne*.
<?
$cart&ne* Cart;
$cart!"add_item(+,-.,,;
$another_cart&ne* Cart;
$another_cart!"add_item(+/--.,0;
?"
7ectorii $items, functiile de adaugare si stergere a elementelor sunt DIFERITE pentru
fiecare obiect in parte (priviti fiecare obiect ca un director separat pe HDD, directoare in
care pot eAista aceleasi fisiere cu continut diferit).
O initiali"are de forma $cart!"items & arra1(2,-2 &" ,) este corecta pentru ca se refera
la variabila items a lui $cart (ca si la vectori indeAati dupa c$ei de tip string).
Prin urmare o apelare $cart!"$items & arra1(2,-2 &" ,), va deveni $cart!"...
Daca dorim ca totusi sa folosim o variabila pentru a returna numele variabilei din clasa,
trebuie mai intai sa declaram $items.
Corect ar fi :
$items&.items.;
$cart!"$items & arra1(2,-2 &" ,)2
Astfel, variabila $items va returna stringul BitemsC, string ce va fi folosit in apelarea
clasei.
Destructorul nu trebuie definit implicit, pentru ca la terminarea scriptului, memoria se
eliberea"a automat. 9otusi, daca dorim simularea unui destructor, putem apela functia
unset pe toate variabilele folosite in functie.
DA:
<?
function 3estro1456ect ($name
{
$theo56ect & 7$894:A9;#$name$;
if (method_e<ists ($theo56ect,23estro12
$theo56ect!"3estro1 (;
unset ($894:A9;#$name$;
'
class <1=
{
var $somevar;
// >>> (his is the constructor
function <1= (
{
'
// >>> (his is the destructor *hich *ill 5e called
function 3estro1 (
{
echo (2?o* *e destro1 it @2;
'
function ;ome3o (
{
echo (2doing somethingA 2B$this!"somevar;
'
'
$e<ample & ne* <1=;
// BBBB doing something here
$e<ample!"somevar & 0;
$e<ample!";ome3o(;
3estro1456ect (2e<ample2;
// >>> (est &" should produce an error @
$e<ample!";ome3o (;
?"
LUCRUL CU FISIERE
Pentru a incepe lucrul, trebuie mai intai sa desc$idem fisierul, folosind functia fopen(,
careia ii atasam un pointer (o variabila ce repre"inta coneAiunea logica cu fisierul).
<?php
$handle & fopen(2/home/rasmus/fileBt<t2, 2r2;
$handle & fopen(2/home/rasmus/fileBgif2, 2*52;
$handle & fopen(2httpA//***Be<ampleBcom/2, 2r2;
$handle & fopen(2ftpA//userApass*ordCe<ampleBcom/somefileBt<t2, 2*2;
?"
Pentru 3indo3s, se dublea"a bacEslas$ Ful :
<?php
$handle & fopen(2cADDdataDDinfoBt<t2, 2r2;
?"
Parametrii de legatura cu fisierul sunt conform tabelului :
'r' Open for reading only; place the file pointer at the beginning of the file.
'r+' Open for reading and writing; place the file pointer at the beginning of the file.
'w'
Open for writing only; place the file pointer at the beginning of the file and truncate the file to
zero length. If the file does not exist, attempt to create it.
'w+'
Open for reading and writing; place the file pointer at the beginning of the file and truncate
the file to zero length. If the file does not exist, attempt to create it.
'a'
Open for writing only; place the file pointer at the end of the file. If the file does not exist,
attempt to create it.
'a+'
Open for reading and writing; place the file pointer at the end of the file. If the file does not
exist, attempt to create it.
Citirea din fisier pentru tip de date alfanumeric se face folosind functia fgets (, pentru
citire de stringuri (pana la primul ne3line) :
<?php
$handle & fopen(2/tmp/inputfileBt<t2, 2r2;
*hile (@feof($handle {
$5uffer & fgets($handle, E-FG;
echo $5uffer;
'
fclose($handle;
?"
Citirea pentru tip de date binar, se face folosind functia fread( , functie ce trebuie sa
aiba ca parametrii de intrare calea catre fisier si numele fisierului, urmat de dimensiunea
pana unde citim (in b,tes).
<?php
// get contents of a file into a string
$filename & 2/usr/local/somethingBt<t2;
$handle & fopen($filename, 2r2;
$contents & fread($handle, filesi=e($filename;
fclose($handle;
?"
Pe sistemele de operare ce nu fac diferenta intre tip de date teAt sau binar (Gindo3s),
fisierul trebuie desc$is cu optiunea b de la binar,.
<?php
$filename & 2cADDfilesDDsomepicBgif2;
$handle & fopen($filename, 2r52;
$contents & fread($handle, filesi=e($filename;
fclose($handle;
?"
Pentru scriere folosind caractere alfanumerice se foloseste functia fputs( care este un
alias la f*rite(, functie folosita pentru date de tip binar.
<?php
$filename & HtestBt<tH;
$somecontent & 2Add this to the fileDn2;
// 9etHs ma)e sure the file e<ists and is *rita5le firstB
if (is_*rita5le($filename {
// In our e<ample *eHre opening $filename in append modeB
// (he file pointer is at the 5ottom of the file hence
// thatHs *here $somecontent *ill go *hen *e f*rite( itB
if (@$handle & fopen($filename, HaH {
echo 2Cannot open file ($filename2;
e<it;
'
// Irite $somecontent to our opened fileB
if (f*rite($handle, $somecontent &&& JA9;K {
echo 2Cannot *rite to file ($filename2;
e<it;
'

echo 2;uccess, *rote ($somecontent to file ($filename2;
fclose($handle;

' else {
echo 2(he file $filename is not *rita5le2;
'
?"

S-ar putea să vă placă și