SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36
  1. #26
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wasn't talking about the underscore ;-)

    ThisIsASampleSentenceCanYouReadIt?

    this_is_a_sample_sentence_can_you_read_it?

    Ruby is about readability, and the second one is much easier to read than the first. And that's why Ruby uses underscores.
    PHP mixes underscores with camelcase in function names. strpos() str_replace(): hard to remember.

    Compare:
    Code:
    string = "this is a string"
    string.include? "string"
    string['string'] = "new string"
    With:
    Code:
    $string = "this is a string";
    strpos($string, 'string') !== false;
    str_replace("string", "new string", $string);
    I have to remember:
    1. base string for strpos is the first argument
    2. base string for str_replace is the last argument
    3. not str_pos, but strpos and not strreplace, but str_replace

    This is much easier in Ruby. Replacing a part of a string is similar to replacing part of an array. Checking if a string contains another string is similar to do such a check on an array:

    Code:
    countries = ['england', 'united states', 'canada']
    countries.include? 'england'
    countries[1] = 'holland'
    Ruby's variable naming:
    Code:
    local_variable = 4
    Constant = 4
    @instance_variable = 4
    @@class_variable = 4
    PHP
    Code:
    $localVariable = 4; // there is no global standard, so $local_variable is ok too
    define('Constant', 4); // function to set constant, eek
    $this->instance_variable = 4; // php uses $this. Ruby doesn't mix methods with properies: everything is a method for the outside world.
    // php doesn't have class variables?
    But you can use pascal/camelcasing if you want:

    Code:
    @instanceVariable
    This is what I originally meant:

    Code:
    public $has_many = "notes";
    vs
    Code:
    has_many :notes
    and

    Code:
    public $belongs_to = array(
    "assoc1" => array("foreign_key"=> "weird_key"),
    "assoc2" => null,
    "assoc3" => array("conditions" => "age > 25"),
    );
    vs
    Code:
    belongs_to :assoc1, :foreign_key => 'weird_key'
    belongs_to :assoc2
    belongs_to :assoc3, :conditions => 'age > 25'
    Rails is much cleaner: you're adding associations, you're not setting class/instance variables.

    Ruby is almost always shorter and more readable.

    Translate this into PHP:

    Code:
    b = true
    list = ('aa'..'zz').select{b = !b}
    List is an array of two strings:
    Code:
    ab
    ad
    af
    ..
    zz
    Who can do that in less than 7 lines in php? And in less than 15 lines in java?
    (1 thing per line, not:
    Code:
    $b = true; $arr  = array(); $str = 'aa'; while($str != 'zz') { $arr[] = $str; $str++; $str++; }
    )

  2. #27
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, I'm starting to get the picture now. The syntax looks good, and its good to KISS. If I use the following code:
    Code:
    isSuccess = true;
    am I creating a constant or a variable?

  3. #28
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A variable. Constants start with a capital letter.

    Code:
    IsSuccess = true
    IsSuccess = false
    Will not work because "I" is a capital letter.

  4. #29
    SitePoint Zealot ChrisCarter's Avatar
    Join Date
    May 2004
    Location
    Wenatchee, WA
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this site a shot neobuddah:

    http://tryruby.hobix.com/

    It'll give you an interactive introduction to Ruby. Kind of fun too

    I think the 'public has_many = "notes"' argument is a little misleading, as Rails is actually using functions (simply without the parenthesis), just as PHP could:

    Code:
    has_many("notes");
    Still not as elegant as Ruby's Symbols, but not as cumbersome as setting variables a million times either.

    Also, I believe you can capitalize class and instance variables without creating constants:

    Code:
    @InstanceVariable
    @@ClassVariable

  5. #30
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think PHP could do that with a function because PHP cannot dynamically add methods to classes from within the class definition.

  6. #31
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Manchester
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    I don't think PHP could do that with a function because PHP cannot dynamically add methods to classes from within the class definition.
    I thought you could now in PHP5 by using reflection?

  7. #32
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't PHP5's reflection read-only?

  8. #33
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    Denmark
    Posts
    1,222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    ... Your model/controllers shouldn't depend on your view. Your view should collect data from your controllers and modeld, but the controllers and models shouldn't be aware of it.
    If you're referring to the Model-View-Controller this is not correct. The controller will indeed know about the structure of the view. In the classic MVC pattern (as pioneered by smalltalk) the controller is responsible for view-specific tasks such as shifting focus, repopulating selection lists of dependant fields, guessing default values etc. All of which requires that the controller "knows" about the view structure, hence the controller will not only depend (in the UML sense) on the view, but it will often hold a reference to the current view.

    On the model I'd agree: The model should know nothing about views or controller. It should expose properties and events for the controller or view to use as they see fit.
    /mouse

  9. #34
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, the Controller is much closer to the view than the models are. But I meant that it isn't good to code your controlles with the HTML of your views in mind. You shoudn't think: "This data is going to be displayed in a table, and not in a list, so I have to do this with it" when coding your controller. It should be possible to display the data in a table and in a list (<ul>) without making changes to the controller.

  10. #35
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    Denmark
    Posts
    1,222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    You shoudn't think: "This data is going to be displayed in a table, and not in a list, so I have to do this with it" when coding your controller. It should be possible to display the data in a table and in a list (<ul>) without making changes to the controller.
    Well said. Coudn't agree more Sorry that I misunderstood you.
    /mouse

  11. #36
    SitePoint Member
    Join Date
    Jan 2005
    Location
    utah
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    so...back to the point

    So, this was about php on trax, not a Rails vs. whatever debate. I think there are several websites devoted to that. Anyone actually try trax?


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
  •