SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question about Class Variable

    What is the different between these two snippets...

    Code:
    class Controller {  
          public $model;   
       
          public function __construct()  {  
               $this->model = new Model();  
          }
    and...

    Code:
    class Controller {  
          public $model;   
       
          public function __construct()  {  
               $model = new Model();  
          }
    Aren't they the same thing?



    TomTees

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    No they're different.

    In the first class you show the model is saved in a variable that is public in the class.
    That is, once you've created an instance of the first Controller, you can call $controller->model and that will give you the model created in the constructor.

    In the second class you show the model is saved in a variable local to the constructor, lot local to the class. So, if I were to create an instance of the second Controller class and call $controller->model i get NULL.

    Makes sense?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What then would be the difference between these two snippets...

    Code:
    class Controller {  
          public $model;
          $this->model = new Model();  
          }
    and...

    Code:
    class Controller {  
          public $model;
          $model = new Model();  
          }
    Aren't they the same thing?



    TomTees

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    In theory yes. However, since logic is only allowed in functions in classes, both examples will give you a PHP error.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    In theory yes. However, since logic is only allowed in functions in classes, both examples will give you a PHP error.
    Hmmm... Interesting...

    So what about this example I did yesterday. Is my code below wrong? (It seems to work?!)


    index.php
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title></title>
    		<meta http-equiv="content-type" content="text/html; charset=utf-8">
    		<link rel="stylesheet" type="text/css" href="index.css">
    	</head>
    	<body>
    		<form method="post" action="time.php">
    			<div>
    				<input type="submit" name="btnSubmit" value="Time" class="btn" id="btnSubmit" />
    			</div>
    		</form>
    	</body>
    </html>

    time.php
    Code:
    <?php
    	include ('classes/class.time.php');
    
    	echo '<pre>';
    	print_r($_POST);
    	echo '</pre>';
    
    	$objTime = new Time;
    	
    	$currTime = $objTime->GenerateCurrentTime();
    	
    	echo "The current time is " . $currTime;
    ?>

    class.time.php
    Code:
    <?php
    	class Time {
    		function GenerateCurrentTime(){
    			$sTime = gmdate("d-m-Y H:i:s");
    			return $sTime;
    		}
    	}
    ?>


    TomTees

  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    In the first example the code is not in a class - valid.
    In the second example the code is in a function in a class - valid.

    In the examples you gave earlier the code was in the class but not in a function in that class, and that's not allowed.

    In a nutshell:

    PHP Code:
    // I can place code here, outside a class
    class X
    {
      
    // I can't put code here
      
    function y()
      {
         
    // but I can place code here
      
    }

    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    SitePoint Evangelist TomTees's Avatar
    Join Date
    Apr 2010
    Location
    Iowa
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification!

    (No wonder it is taking me so long to learn PHP and OOP?! I always seems to root out these little nuances?!)



    TomTees


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •