PDA

View Full Version : PHP OO Inheritance



Hypertext
18-10-2008, 09:26 PM
I've seen people using inheritance in PHP two different ways, thought I imagine there is more.

Like this:


<?php

public class NumberOne {}
public class NumberTwo extends NumberOne {}
// Number two has access to NumberOne's members within scope
?>

And this:
[/php]
<?php
public class NumberOne {}
public class NumberTwo {
$this->two = new NumberOne;
$this->two has access to NumberOne's members within scope
}
?>
[/php]

Sorry for any syntax errors, but you get the idea.

I'm coding a new application that is quite complicated, and was wondering how I should code my application?

I've seen both of these in reasonably upscale code, so I presume it's OK with both.

Could you list the pros and cons?

Thanks,

Charlie.

Source
18-10-2008, 09:39 PM
Oh gawd. I can't be bothered really but i'll explain one thing with the first method (using extends) - I believe you can only extend it once... meaning you may be stranded trying to access other classes etc...

And please, please stop using technical knowledge - only because when you writing it, it seems so fake.

Hypertext
18-10-2008, 10:39 PM
Oh gawd. I can't be bothered really but i'll explain one thing with the first method (using extends) - I believe you can only extend it once... meaning you may be stranded trying to access other classes etc...

And please, please stop using technical knowledge - only because when you writing it, it seems so fake.

OK, anything else?

Also, stop telling me that, I know what I'm saying when I say it. :/

Source
18-10-2008, 11:12 PM
I'm just saying when your using that type of language it feels as though you are trying desperatly to be known as some coding god. I'm sure people understand where im coming from.

VistaBoy
18-10-2008, 11:32 PM
I usually pas on things like this, sort of like your 2nd example, but it has nothing to do with inheritance

Here is an example:

configuration:



require_once "classes/core.php";

$database ["server"] = "localhost";
$database ["username"] = "";
$database ["password"] = "";
$database ["database"] = "";

$core = new core( );
$core->init( $database );
init in core:



public function init($database) {
$server = $database ["server"];
$username = $database ["username"];
$password = $database ["password"];
$databaseName = $database ["database"];

require_once "classes/database.php";
$database = new database( );
$database->core = & $this;

$this->database = & $database;

/*
* Define error types.
*/

$this->errorType = array ();
$this->errorType ["error"] = E_USER_ERROR;
$this->errorType ["warning"] = E_USER_WARNING;
$this->errorType ["notice"] = E_USER_NOTICE;

/*
* Connect
*/
$database->init();
$database->connectMySQL( $server, $username, $password, $databaseName );
}
I set references to classes so, $this->database = & $database, $this->template = & $template.

Why do you not define $database like



require_once "classes/core.php";
$database = array ( );
$database ["server"] = "localhost";
$database ["username"] = "";
$database ["password"] = "";
$database ["database"] = "";

$core = new core( );
$core->init( $database );


or is it pointless in doing that?

Jackboy
19-10-2008, 12:10 AM
I'm just saying when your using that type of language it feels as though you are trying desperatly to be known as some coding god. I'm sure people understand where im coming from.

Yeh, u talking about when he said sumthing about

MySQLi or something?

Dentafrice
19-10-2008, 12:31 AM
I usually pas on things like this, sort of like your 2nd example, but it has nothing to do with inheritance

Here is an example:

configuration:



require_once "classes/core.php";

$database ["server"] = "localhost";
$database ["username"] = "";
$database ["password"] = "";
$database ["database"] = "";

$core = new core( );
$core->init( $database );


init in core:



public function init($database) {
$server = $database ["server"];
$username = $database ["username"];
$password = $database ["password"];
$databaseName = $database ["database"];

require_once "classes/database.php";
$database = new database( );
$database->core = & $this;

$this->database = & $database;

/*
* Define error types.
*/

$this->errorType = array ();
$this->errorType ["error"] = E_USER_ERROR;
$this->errorType ["warning"] = E_USER_WARNING;
$this->errorType ["notice"] = E_USER_NOTICE;

/*
* Connect
*/
$database->init();
$database->connectMySQL( $server, $username, $password, $databaseName );
}


I set references to classes so, $this->database = & $database, $this->template = & $template.

Tomm
19-10-2008, 12:35 AM
The second example is not a example of inheritance.

Hypertext
19-10-2008, 04:16 PM
Ok. Thanks guys. Source I understand what you are saying, but I'm just using that kind of language so it's easier to explain to people.

Want to hide these adverts? Register an account for free!