Jump to content

SystemOverload

Members
  • Posts

    28
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

SystemOverload's Achievements

Member

Member (2/5)

0

Reputation

  1. So why were the properties assigned values in the example where the child had no constructor???
  2. Thorpe Thanks, but why were there parent's variables available in the normal methods, but not the constructor. Without the parent::__constructor() was 'echo $this->protected;' effectively redeclaring it (and consequentially setting it's value to nothing) in the child object?
  3. JC, you can, because in both examples I am accessing properties from the parent object, just in the second example I try to access those from the child's constructor first, which seems to erase the contents in the child. ???
  4. I'm doing some basic object coding to get my head around OOP PHP, but I've come across something I can't get to work. I've got two identical peices of code, MyClass is defined, MyClass2 is then defined as an extension of MyClass. MyClass has three properties Public, Private and Protected. In my first example I can access Public and Protected via a method in MyClass2, this class does NOT have a constructor. The second example has a constructor but I cannot access Public: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <body> <?php ECHO "MyClass2 does not have a constructor, Public & Protected both echo correctly from MyClass2->printHello!!!<BR>"; /** * Define MyClass */ class MyClass { public $public; protected $protected; private $private; protected $varXXX; function __construct() { $this->public = "Public"; $this->protected = "Protected"; $this->private = "Private"; //$this->printHello(); } function printHello() { echo $this->public . "!!!<BR>"; echo $this->protected . "!!!<BR>"; echo $this->private . "!!!<BR>"; } } /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; /* function __construct() { echo $this->public . "#<BR>"; echo $this->protected . "#<BR><BR>"; //echo $this->private . "???<BR>"; } */ function printHello() { echo $this->public . "???<BR>"; echo $this->protected . "???<BR><BR>"; //echo $this->private . "???<BR>"; } } $obj = new MyClass(); //echo $obj->public . "<BR>"; // Works //echo $obj->protected; // Fatal Error //echo $obj->private; // Fatal Error //$obj->printHello(); // Shows Public, Protected and Private $obj2 = new MyClass2(); //echo $obj2->public; // Works //echo $obj2->private; // Undefined //echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined ?> </body> </html> RESULTS IN: Public??? Protected??? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <body> <?php ECHO "MyClass2 DOES have a constructor, Public does not work from MyClass2->printHello!!!<BR>"; /** * Define MyClass */ class MyClass { public $public; protected $protected; private $private; protected $varXXX; function __construct() { $this->public = "Public"; $this->protected = "Protected"; $this->private = "Private"; //$this->printHello(); } function printHello() { echo $this->public . "!!!<BR>"; echo $this->protected . "!!!<BR>"; echo $this->private . "!!!<BR>"; } } /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected method, but not private protected $protected = 'Protected2'; function __construct() { echo $this->public . "#<BR>"; echo $this->protected . "#<BR><BR>"; //echo $this->private . "???<BR>"; } function printHello() { echo $this->public . "???<BR>"; echo $this->protected . "???<BR><BR>"; //echo $this->private . "???<BR>"; } } $obj = new MyClass(); //echo $obj->public . "<BR>"; // Works //echo $obj->protected; // Fatal Error //echo $obj->private; // Fatal Error //$obj->printHello(); // Shows Public, Protected and Private $obj2 = new MyClass2(); //echo $obj2->public; // Works //echo $obj2->private; // Undefined //echo $obj2->protected; // Fatal Error $obj2->printHello(); // Shows Public, Protected2, Undefined ?> </body> </html> RESULTS IN: # << Missing the property's contents Protected2# ??? << Missing the property's contents Protected2??? Any ideas why I cannot access the property's contents in the second example when the only difference is that it has a constructor?
  5. 1 and 2 didn't seem to work and I didn't like the look of 3, so steered clear. I went with extending ClassB from ClassA. Instantiated ClassB, which had a constructor that called ClassA's constructor. This then allowed me to call ClassA's variable from inside CLassB as "$this->variable".
  6. Apologies, I thought I'd been quite clear in my question. Let me layout exactly what I want to know is possible or not: class clClassA { public varA; function clClassA { ...blah... $this->varA = 999; ...blah... } function fnFunctionA { <does something else> } } class clClassB { function clClassB { echo $objA::varA; } } objA = new clClassA; objB = new clClassB; I want objB to be able to access objA::varA or execute objA::fnFunctionA IE: access one class' properties/methods from within another class' methods. I do not want to instigate things or access properties from outside of the classes. Is my question clearer now? Thnx
  7. Thanks, but that didn't help. The properties are public, or at least some are, but the object is not recognised: Notice: Undefined variable: objSession in C:\WEBSERVER\system\classes\clAccount.class on line 9 Notice: Trying to get property of non-object in C:\WEBSERVER\system\classes\clAccount.class on line 9 Can you give me a yes/no? Should I be able to access Class A's properties and methods from Class B's methods?
  8. Hi Can you call Class A's methods or properties from Class B's methods? Thanks.
  9. ok, thanks for everyone's input, especially the last guy, upto the point where he posted, I didn't get exactly what i asked for... If you read my original request: Everyone except the last guy dropped titbits, but didn't give me exactly what I needed, finally I finished up with the following: (credit for the substr() bit goes to a random tutorial I found thru google) $varQuery = ""; if (strlen($_SERVER['QUERY_STRING']) != 0) {$varQuery = "?";} $varURL = substr($_SERVER['SERVER_PROTOCOL'],0,strpos($_SERVER['SERVER_PROTOCOL'],'/')) . "://" . $_SERVER["HTTP_HOST"] . $_SERVER['SCRIPT_NAME'] . $varQuery . $_SERVER['QUERY_STRING'] ; Thanks for everyones help.
  10. Trouble is _SERVER is an array and parse_url takes a string... How do i get from _SERVER to "http://www.domain.com/dir1/dir2/something.php?id=1&mode=pwc" Sorry for being a pain, lol Thanks
  11. I'm sorry, I didn't make myself clear. How do I retrieve the seperate parts of the url, parse_url() is just a function, how do I get the URL itself into a variable so I can start manipulating it? Thanks
  12. PHP 5.2.5 I want to create a function to manipulate the current URL. How is the best way to retrieve the separate parts of the URL to a variable. 1 - "HTTP" or "HTTPS" etc 2 - www.domain.com 3 - directory/2nddirectory 4 - index.php 5 - id=10&mode=qv I want to be able to insert and/or remove 'variables' to facilitate passing more information thru _GET Thanks
  13. I've started with the basic mail(), quickly finding it doesn't provide much feedback in case of errors, such as the mail server is offline/not responding... I now need a function/class which allows me to specify a domain/ip as the mailserver and one which if it can't talk to the server, gives me the chance to provide a dignified error message back to the user etc... Any ideas guys? Thanks
  14. Hey I'm on PHP 5.3.4ish. I've recently started using PHPMailer, but when testing it, end up with an "Could not instantiate mail function." error. Does anyone have any ideas? My files are laid out as below: /emailtest.php - Calls fn_mailer("test@live.co.uk","Test Email Recipient","Subject","BODY") /includes/functions/fn_comms.php - Contains function fn_mailer(), a simple wrapper that accesses the PHPMailer Functionality... /includes/functions/phpmailer/class.phpmailer.php /includes/functions/phpmailer/class.pop3.php /includes/functions/phpmailer/class.smtp.php fn_mailer() function fnMailer ($varToEmail, $varToName, $varSubject, $varBody) { // - -- --- Wrapper for PHP Mailer Class Interface ------------------------------------ require_once 'phpmailer/class.phpmailer.php'; $mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch try { $mail->AddReplyTo('support@test.info', 'Support'); $mail->AddAddress($varToEmail, $varToName); $mail->SetFrom('donotreply@test.info', 'Admin'); $mail->Subject = $varSubject; $mail->AltBody = 'To view this email message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically //$mail->MsgHTML(file_get_contents('contents.html')); $mail->MsgHTML($varBody); //$mail->AddAttachment('images/phpmailer.gif'); // attachment //$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment $mail->Send(); echo "Message Sent OK</p>\n"; } catch (phpmailerException $e) { echo $e->errorMessage(); //Pretty error messages from PHPMailer } catch (Exception $e) { echo $e->getMessage(); //Boring error messages from anything else! } // - -- --- END OF WRAPPER ------------------------------------------------------------------ }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.