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: 80
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Na początku, kiedy zobaczyłem, że ktoś chce jakiejś opłaty za pomoc w tworzeniu stron ryknąłem śmiechem - potem przyszły problemy... i zaryzykowałem. Druga rzecz to: nie chciałem "kopiować". Ale prawda jest taka: są lepsi, bardziej doświadczeni i... czasem trzeba poprosić o pomoc, a jak poświęca się na to trzecią cześć życia, to nic dziwnego, że nie chce się swoich "sekretów" zdradzać za darmo. Skorzystałem z "algorytmy.pl" i naprawdę jestem z tego w 100% zadowolony, polecam - dla zawodowców (co się uczą) i amatorów (można skorzystać z gotowego rozwiązania).

Tomasz Czypicki
Cybernoxa

   
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]

Obiekty (object)

Inicjowanie obiektów

Aby zainicjować obiekt, należy użyć słowa kluczowego new aby przypisać instancję obiektu do zmiennej.

<?php
class foo
{
   function
do_foo()
   {
       echo
"Doing foo.";
   }
}

$bar = new foo;
$bar->do_foo();
?>

Pełna informacja na ten temat znajduje się w rozdziale Klasy i Obiekty.




User Contributed Notes

Trevor Blackbird > yurab.com
27-Nov-2005 06:33

You can create a new object using the built-in stdClass or by using type-casting:

<?php

// This is the proper way
$object1 = new stdClass();

// This works too
$object2 = (object) NULL;

// This will create an object from an array
$monkey_array = array('title'=>'Spider Monkey', 'src'=>'monkey.jpg');
$monkey_object = (object) $monkey_array;
print
$monkey_object->title . ' ' . $monkey_object->src;

// You can type-cast in the middle of an expression
function customHTML($some_object) {
// this function expects an object as the argument and returns some output
}
print
'<p>Writing some output ' . customHTML( (object) array('rows'=>3, 'cols'=>4) );

?>


james dot jones at firstinvestors dot com
10-Mar-2005 06:32

iblun:

Highly recommended that you NOT try to write your own sort function. Try something like this instead:

<?php

function sort_by_field($obj_array, $field)
{
   return
usort($obj_array,
        
create_function('$o1,$o2',
            
"return (\$o1->$field < \$o2->$field) ? -1 : 1"));
}

?>

(Warning: untested code...)


iblun at gmx dot net
09-Mar-2005 02:08

To sort an array, that contains an object, after one fieldname inside the object, im using this function:

function objectSort($objectarray, $field)
{
   for ($a=0;$a < (count($objectarray)); $a++)
   {
       for ($b=0;$b < (count($objectarray)); $b++)
       {   
           if ($objectarray[$a]->$field < $objectarray[$b]->$field)
           {
               $temp = $objectarray[$a];
               $objectarray[$a] = $objectarray[$b];
               $objectarray[$b] = $temp;
           }
       }
   }
  
   return $objectarray;
}


mortoray at ecircle-ag dot com
16-Feb-2005 11:07

If you use new to create items in an array, you may not get the results you want since the parameters to array will be copies of the original and not references.

By Example:
class Store {
   var $item = 3;
}

   $a = array( new Store() );
   $b = $a;
   $a[0]->item = 2;
   print( "|" . $b[0]->item . "| <br>" );  //shows 3

   $a = array();
   $a[] =& new Store();
   $b = $a;
   $a[0]->item = 2;
   print( "|" . $b[0]->item . "| <br>" );  //shows 2

This is extremely important if you intend on passing arrays of classes to functions and expect them to always use the same object instance!

Note: The following syntax is desired (or maybe even the default notation should translate as this):
   $a = array( &new Store() );


sirbinam at nospam dot please dot hotmail dot com
16-Nov-2004 03:46

<?php

class hack{}
$hack =& new hack;

class
profiler{
  function
profiler(){
  
$this->startime = microtime();
  }

  function
dump(){
   global
$hack;
  
$this->endtime = microtime();
  
$duration = $this->endtime - $this->starttime; /* this won't actually work, it just the concept */
  
$stdout->write($duration);
  }
}

class
stdout{
  function
write($msg){
   echo
$msg;
  }
}

$stdout =& new stdout();
$hack =& $stdout;
$profiler->dump();

?>

/*
     *
     * In short this little hack allows us to call $stdout->write() from
     * $profiler->dump
     *
     * The problem is that $stdout doesn't exist yet and when the compiler
     * parses this class, it sends a fatal error and dies because you can't
     * refer to a method of an object that doesn't exist yet, even though
     * this method doesn't get called until the end of execution, when the
     * method does exist.
     * This is the same as not being able to use a function before it is
     * at least declared in say a header file. This is seen in C, Perl,
     * and pretty much every language known to man. (TTBOMK?)
     *
     * So what does this hack do?
     * The first thing that happens in the global scope is an empty class
     * definition, it then creates a object called $hack from this class.
     * All this does is allocate memory for an object, and places a pointer
     * at the begining of that memory segment.
     * When the compiler parses this class, it doesn't care that the $hack
     * object is empty, as long as it has somewhere to assign a function
     * pointer. Later in global scope the $stdout object is created.
     * After $stdout is created, we do $hack =& $stdout. The =&
     * (assign by reference) moves the pointer for $hack to the begining of
     * the memory segment for $stdout. So when we call $hack->write(), it
     * points to the exact same object->method() as $stdout->write(). So
     * this is actually very reliable, just don't tell a purist!
     */


jbinam at nospam dot please dot hotmail dot com
16-Nov-2004 03:46

<?php

class hack{}
$hack =& new hack;

class
profiler{
  function
profiler(){
  
$this->startime = microtime();
  }

  function
dump(){
   global
$hack;
  
$this->endtime = microtime();
  
$duration = $this->endtime - $this->starttime; /* this won't actually work, it just the concept */
  
$stdout->write($duration);
  }
}

class
stdout{
  function
write($msg){
   echo
$msg;
  }
}

$stdout =& new stdout();
$hack =& $stdout;
$profiler->dump();

?>

/*
     *
     * In short this little hack allows us to call $stdout->write() from
     * $profiler->dump
     *
     * The problem is that $stdout doesn't exist yet and when the compiler
     * parses this class, it sends a fatal error and dies because you can't
     * refer to a method of an object that doesn't exist yet, even though
     * this method doesn't get called until the end of execution, when the
     * method does exist.
     * This is the same as not being able to use a function before it is
     * at least declared in say a header file. This is seen in C, Perl,
     * and pretty much every language known to man. (TTBOMK?)
     *
     * So what does this hack do?
     * The first thing that happens in the global scope is an empty class
     * definition, it then creates a object called $hack from this class.
     * All this does is allocate memory for an object, and places a pointer
     * at the begining of that memory segment.
     * When the compiler parses this class, it doesn't care that the $hack
     * object is empty, as long as it has somewhere to assign a function
     * pointer. Later in global scope the $stdout object is created.
     * After $stdout is created, we do $hack =& $stdout. The =&
     * (assign by reference) moves the pointer for $hack to the begining of
     * the memory segment for $stdout. So when we call $hack->write(), it
     * points to the exact same object->method() as $stdout->write(). So
     * this is actually very reliable, just don't tell a purist!
     */


nconantj
18-Jul-2004 09:43

php at electricsurfer.com,

More than a year later and here's some clarification of what's happening in your code, via comments in an otherwise verbatim copy.

<?
class c
{
   var
$a = array('a'=>'aa','b'=>'ab');
   var
$b = 'c';
 
   function
show()
   {
       echo
$this->a['a']; // -> 1st
      
echo $this->a['b']; // outputs 'ab'
    
      
$a = 'a';
      
$b = 'b';
    
       echo
$this->$a[$a]; // [] 1st, not what I expected
       //Above first becomes $this->$a['a'] by looking at the function's local $a
       //Next it becomes $this->a by again looking at the function's local $a, which references the class variable $a with no subscripts.
       // In order to reference elements of the class variable $a,
       // you want to use $this->a[$a]

      
echo $this->$a[$b]; // does NOT output 'ab'
       // Same as above, but the first step $b becomes 'b'

      
$this_a =& $this->$a; // work-around
    
      
echo $this_a[$a]; // no question
      
echo $this_a[$b];
    
      
$a_arr = array('a'=>'b');
    
       echo
$this->$a_arr[$a]; // [] 1st => outputs 'c'
       // This becomes $this->$a_arr['a'] which becomes $this->c,
       // by referencing the local variables first.
  
}
}
$c = new c();
$c->show();
?>


info at keltoi-web dot com
25-Aug-2003 11:26

PHP supports recursive type definitions as far as I've tried. The class below (a _very_ simple tree) is an example:

class Tree {

var $_value = null;
var $_children = array();

function Tree ($value) {
  $this->_value = $value;
}

function addChild ($value) {
  $aux_node = new Tree ($value);
  $this->_children [] = $aux_node;
  return $aux_node;
}
}

As you can see, in addChild we reference Tree again...

However, you must be careful about references. See the chapter "References explained" for more details.

Hope this helps.


php at electricsurfer dot com
22-May-2003 06:25

Here's an example on operator precedence between ->  and []
& what happens with $object->$member[$array_element]

<?
class c
{
   var
$a = array('a'=>'aa','b'=>'ab');
   var
$b = 'c';
  
   function
show()
   {
       echo
$this->a['a']; // -> 1st
      
echo $this->a['b']; // outputs 'ab'
      
      
$a = 'a';
      
$b = 'b';
      
       echo
$this->$a[$a]; // [] 1st, not what I expected
      
echo $this->$a[$b]; // does NOT output 'ab'
      
      
$this_a =& $this->$a; // work-around
      
      
echo $this_a[$a]; // no question
      
echo $this_a[$b];
      
      
$a_arr = array('a'=>'b');
      
       echo
$this->$a_arr[$a]; // [] 1st => outputs 'c'
  
}
}
$c = new c();
$c->show();
?>


 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt