Forum Moderators: coopster

Message Too Old, No Replies

$this->load->function(parameter) - what is is ?

         

adresanet

5:05 pm on May 29, 2015 (gmt 0)

10+ Year Member



Hi,

Reading CodeIgnitier documentation I saw some statements having this form:
$this->someword->somefunction();
and I don't know what is that.

Is someword an multi-dimensional/associative array?
I know that if I create an instance of a class I can access its properties and functions with a single -> operator or with :: operator.
But I don't know what means usage of 2 -> parameters.

Thanks!

whitespace

6:38 pm on May 29, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Well, you would seem to know the answer already, although it's not an "array", it's a class instance (an object).

I know that if I create an instance of a class I can access its properties and functions with a single -> operator...


It's really the same thing...

someword is a property of the current class ($this refers to the current class) that holds a class instance. somefunction is a method of that class instance.

At some point earlier in the code a class instance has been assigned to the property someword.

There's no limit to how "deep" you can go...

$someVar->A->B->C->D->methodOfD();

Where A, B, C and D are all class properties that hold an instance of a class.

adresanet

7:12 pm on May 29, 2015 (gmt 0)

10+ Year Member



Thank you for your response!
If you can please write for me a class implementation and usage so I can understand exactly how I can use them.

whitespace

10:10 pm on May 29, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



An example... an AddressBook class and an Emailer class, which sends an email to a user whose ID is found in the AddressBook.

We only use the object/arrow operator here since we are dealing with instantiated classes (objects). The :: (double colon OR scope resolution operator) is used to reference static properties and methods (or classes directly... classes which have not been instantiated).


/**
* Our address book class
*/
class AddressBook implements IAddressBook {
/**
* Get the fullname of the passed user
* @param string $userId A registered user's ID
*/
public function getFullname($userId) {
$fullname = null;
/* Lookup users fullname for user $userId, throw Exception on error, etc. */
return $fullname;
}
public function getEmailAddress($userId) {
$emailAddr = null;
/* Lookup primary email address for user $userId */
return $emailAddr;
}
public function getTelephoneNumber($userId) {
/* Etc. */
}
}

/**
* A class for emailing users (from our address book)
*/
class Emailer {
/**
* Class property holds an instance of our address book
* which is passed into the constructor (when the class is instantiated)
*/
protected $_addressBook = null;

/**
* Default "From" address
* - Override in extended class
*/
protected $_fromAddress = 'noreply@example.com';

/**
* Class constructor
* @param IAddressBook $addressBook An instance (ie. object) of our address book
*/
public function __construct(IAddressBook $addressBook) {
// Assign passed object to our class property
// "this" is a special variable referring to the current class instance
$this->_addressBook = $addressBook;
}

/**
* Send welcome email to user
* @param string $userId A registered user's ID of the user we wish to send the email to
*/
public function sendWelcome($userId) {
// ** MAGIC **
// Lookup details about user in our address book
$userName = $this->_addressBook->getFullname($userId);
$userEmail = $this->_addressBook->getEmailAddress($userId);

$body = <<<EOD
Hi $userName,
Welcome to WebmasterWorld!
EOD;
}
mail($userEmail, 'Welcome to WebmasterWorld', $body, 'From: '.$this->_fromAddress);
}

// Instantiate the AddressBook
$addressBook = new AddressBook();

// Instantiate the Emailer, passing our already instantiated AddressBook to the constructor
$emailer = new Emailer($addressBook);

// Send a welcome email to a user whose ID is...
$emailer->sendWelcome('whitespace');


Not sure why the syntax highlighting appears to have stopped half way through the code? (The forum appears not to be able to handle it correctly?!)

whitespace

10:29 pm on May 29, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Btw, search for "MAGIC" in the above code block to see the use of the double -> (object/arrow) operator.

adresanet

7:24 am on May 30, 2015 (gmt 0)

10+ Year Member



Thank you!
It is very clear now for me. Actually it has some logic, but at first I was scared of it :)