Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 67
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Prawdziwa skarbnica wiedzy na temat tworzenia stron WWW i nie tylko. Korzystam z porad praktycznie codziennie, jest mi to niezbędne w mojej pracy. Sam zajmuję się tworzeniem serwisów, ale porady pisane przez Darka sa dla mnie nieocenioną pomocą! Proste, czytelne i zrozumiałe dla każdego! Czekam na więcej!

Krzysztof Szypulski
KESS - projektowanie stron

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

Rozdział 19. Classes and Objects (PHP 5)

Introduction

In PHP 5 there is a new Object Model. PHP's handling of objects has been completely rewritten, allowing for better performance and more features.




User Contributed Notes

epeterson5 at student dot gsu dot edu
07-Oct-2005 07:46

This isn't explicity noted anywhere, I don't think.
If you use a variable after the "->" part of a class attribute call, it will parse the variable as a string to assign/compare that class attribute:

<?php
class User {
   var
$test;
   var
$test2;
}
 
$user1 = new User();
$user2 = new User();
$user1->test = "user1's test";
$user1->test2 = "user1's test2";
$user2->test = "user2's test";
$user2->test2 = "user2's test2";

print_r($user1); print_r($user2);

foreach(
$user1 as $key => $value)
  
$user2->$key = $value;

print_r($user1); print_r($user2);
?>

Which outputs:

User Object: user1
   [test] => user1's test
   [test2] => user1's test2
User Object: user2
   [test] => user2's test
   [test2] => user2's test2
User Object: user1
   [test] => user1's test
   [test2] => user1's test2
User Object: user2
   [test] => user1's test
   [test2] => user1's test2

This is particularly useful when you want to assign a different instance of the same class to $this- I have a standard method that I call when I need to do that:

<?php

  
function assignToThis($new)  {
       foreach(
$new as $key => $value)
          
$this->$key=$value;
   }
?>


turgut85 at hotmail dot com
30-Jun-2005 03:04

<?PHP

// How to hold objects in an arrayList and retrieve it //
// Interface implementation //

class Data {
  
  
public $Name;
  
public $Age;
  
public $Address;
  
  
public function __construct($Name,$Age,$Address) {
      
$this->Name = $Name;
      
$this->Age = $Age;
      
$this->Address = $Address;
   }
  
  
public function __destruct() {
       echo
"Default Constructor...\n";
   }

}

$ArrayList = array();

$ArrayList[0] = new Data("John McDonald","65","Address");
$ArrayList[1] = new Data("Turgut Z. Yesilyurt","30","NJ, USA");
$ArrayList[2] = new Data("Maria ","25","NJ, USA");

print_r($ArrayList);

$obj1 = $ArrayList[1];
echo
"Name : ".$obj1->Name."\n";
echo
"Age  : ".$obj1->Age."\n";
echo
"Address : ".$obj1->Address."\n";

// Interface Usage //
interface ShowMe {
  
public function ShowName();
}

class
ShowData implements ShowMe {
  
public $obj;
public function __construct($Obj) {
  
$this->obj = $Obj;
  
$this->ShowName();
}

public function ShowName() {
   echo
"Age  : ".$this->obj->Age."\n";
}
}

echo
"\nDisplay Single Data =========================\n\n";
new
ShowData($ArrayList[1]);
echo
"\n=============================================\n";

// Turgut Z. YESILYURT, MS
// Software Developer //
// NJ, USA  //

?>

Array
(
   [0] => Data Object
       (
           [Name] => John McDonald
           [Age] => 65
           [Address] => Address
       )

   [1] => Data Object
       (
           [Name] => Turgut Z. Yesilyurt
           [Age] => 30
           [Address] => NJ, USA
       )

   [2] => Data Object
       (
           [Name] => Maria
           [Age] => 25
           [Address] => NJ, USA
       )

)
Name : Turgut Z. Yesilyurt
Age  : 30
Address : NJ, USA

Display Single Data =========================

Age  : 30

=============================================
Default Constructor...
Default Constructor...
Default Constructor...


zabmilenko at hotmail dot com
27-Jun-2005 09:27

Dynamic instantiation trick:

<?php

class CITY
{
  
private $population;

  
public function __construct($cityname)
   {
      
// Load some city-specific data
  
}

  
public function population($demographic = 'all')
   {
       return
$this->population[$demographic];
   }
}

class
COUNTRY
{
  
private $code = null;
  
private $cities = array();

  
public function __construct($code)
   {
      
$this->code = $code;
   }

  
public function city($cityname)
   {
       if (!
$this->cities[$cityname])
       {
          
$this->cities[$cityname] = new CITY($cityname);
       }

       return
$this->cities[$cityname];
   }
}

class
WORLD
{
  
private $countries = array();

  
public function country($code = 'us')
   {
       if (!
$this->countries[$code])
       {
          
$this->countries[$code] = new COUNTRY($code);
       }

       return
$this->countries[$code];
   }
}

$world = new WORLD;

// Load the country AND city object
echo $world->country('us')->city('seattle')->population('employed');

// Country US is already loaded, only need to load a new city object.
echo $world->country('us')->city('new york')->population();

?>

This example uses Countries and Cities wrapped around a World object.  You can use any schema you want, however.  Think:  Domain->Subdomain->Node or KINGDOM->PHYLUM->CLASS->ORDER->FAMILY->GENUS->SPECIES

What is happening here is that a private array is storing the class objects.  Only the class objects that are needed are loaded, when they are needed.  You can nest this many many times as needed.

You see that the array is never exposed.  Each function reference will check to see if a new class object is needed and create it if necessary.

This is literally as simple as it looks.  Hope it helps someone out.


Obeliks
10-Jun-2005 11:56

You can call parent::__construct(), even if the class yours inherits from uses the old constructor style "Classname()".

Example:

<?php

class A {
  function
A() {
   echo
'Constructor of A<br/>';
  }
}

class
B extends A {
  function
__construct() {
  
parent::__construct();
   echo
'Constructor of B<br/>';
  }
}

class
C extends A {
  function
__construct() {
  
parent::A();
   echo
'Constructor of C<br/>';
  }
}

$b = new B();
echo
'<br/>';
$c = new C();

/* will output:
Constructor of A
Constructor of B

Constructor of A
Constructor of C
*/

?>

So you see you can also call parent::Classname() if your superclass uses this format. Keep in mind that it doesn't work the other way round though:

<?php

class A {
  function
__construct() {
   echo
'Constructor of A<br/>';
  }
}

class
B extends A {
  function
__construct() {
  
parent::A();
   echo
'Constructor of B<br/>';
  }
}

$b = new B();

/* will output:
Fatal error: Call to undefined method A::a() in __FILE__ on line __LINE__
*/

?>

So it's always the best choice to use the __construct style! (at least if you're running PHP5)


bartlewis at gmail dot com
08-Jun-2005 03:29

In regards to gaehngaehn at hotmail dot com...

More simply, to create an object from a class using a variable, just try the following.

$a = new $class();

Works in php4 and php5.


S

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt