|
użytkowników online: 67
|
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
|
|
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)
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 Notesepeterson5 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
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 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";
?>
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)
{
}
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;
echo $world->country('us')->city('seattle')->population('employed');
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();
?>
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();
?>
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
|