Jump to content
php.lv forumi

oop klases.


anonīms

Recommended Posts

Believe or not, bet esmu saņēmies kko sākt darīt ar klasītēm :D

 

Tātad

class league {

   // get league admins
    function admins($lid) {
        $league_admins = queryy("SELECT ".PREFIX."lietotaji.lietotajvards FROM ".PREFIX."lietotaji, ".PREFIX."league_admins
                                 WHERE ".PREFIX."league_admins.lid = '".$lid."' AND ".PREFIX."league_admins.uid=".PREFIX."lietotaji.id
                                 ORDER BY ".PREFIX."lietotaji.lietotajvards ASC");
        while($admin = mysql_fetch_object($league_admins)) {
         print $admin->lietotajvards.'<br />';
        } 
     }

}

 

un izvade

$league = new league();
$league->lid=$league['id'];
$league->admins();

 

problēma ir tāda, ka īsti nemāku padot to $lid priekšs f-jas.

 

Mēģināju gan kā piemērā, gan

$league->admins($league['id']); 

Kā tad īsti pareizi ir?

Link to comment
Share on other sites

class league {

public $lid;
   // get league admins
    function admins() {
        $league_admins = queryy("SELECT ".PREFIX."lietotaji.lietotajvards FROM ".PREFIX."lietotaji, ".PREFIX."league_admins
                                 WHERE ".PREFIX."league_admins.lid = '".$lid."' AND ".PREFIX."league_admins.uid=".PREFIX."lietotaji.id
                                 ORDER BY ".PREFIX."lietotaji.lietotajvards ASC");
           $output = '';
        while($admin = mysql_fetch_object($league_admins)) {
           $output .= $admin->lietotajvards.'<br />';
        } 

        return $output;
     }

}

 

par otro punktu nesapratu. Varbūt varētu piemēru kā izvadīt, ja ir pieejams $league['id']. ?

Link to comment
Share on other sites

Cik nu pareizi, bet...

 

echo $league->admins($league['id']);

 

1. Klasē nav mainīgā lid (private $lid; vai public $lid;), atbilstoši piešķirt viņu nevar.

 

Varēt jau var, ja ir maģiskā metode __set().

Link to comment
Share on other sites

@rpr

 

Metodēm nevajadzētu "drukāt". Izmanto "return".

 

@marcis

 

http://php.net/manual/en/language.oop5.visibility.php

 

Ja nav norādīta redzamība, tad tā ir "public". Tāpēc nav īpašas jēgas rakstīt "public" visu laiku priekšā...

 

/**
* Define MyClass
*/
class MyClass
{
   // Declare a public constructor
   public function __construct() { }

   // Declare a public method
   public function MyPublic() { }

   // Declare a protected method
   protected function MyProtected() { }

   // Declare a private method
   private function MyPrivate() { }

   // This is public
   function Foo()
   {
       $this->MyPublic();
       $this->MyProtected();
       $this->MyPrivate();
   }
}

$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work

Link to comment
Share on other sites

1. Klasē nav mainīgā lid (private $lid; vai public $lid;), atbilstoši piešķirt viņu nevar.

2. Izskatās ka tev kļūdaini tiek izmantots viens un tas pats mainīgais masīvam un klasei. Vismaz nosaukumi viņiem vienādi.

 

 

Cik nu pareizi, bet...

 

echo $league->admins($league['id']);

 

 

 

Varēt jau var, ja ir maģiskā metode __set().

 

Kaa nu nevar - PHP to var:

 


<?php
class Test {}

$test = new Test;

var_dump($test);

$test->property = 'OMG THIS IS IMPOSSIBLE';

var_dump($test);

 

Protams tas nenoziimee ka taa vajag dariit...

 

Un jaa - 'pieejas liimeni' (public/protected/private) vajag noraadiit konstanti nevis shad tad likt un shad tad nee.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...