Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

HTML form and PHP V1

 
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Stop asking about your main problem. We'll get there eventually, if you answer my questions and follow my instructions.

So, you know that you use the new keyword to create an instance. I told you to create an instance of type Person and assign it to a variable named $author. I also told you to create an instance of type Book, and pass the value of the $author variable to its constructor call.

Do that now. The code should only be two lines long.  I don't want to see any class definitions.
 
Ranch Foreman
Posts: 342
  • Mark post as helpful
  • send pies
  • Report post to moderator
I told you to create an instance of type Person and assign it to a variable named $author. (below is the code for this)

$author = new Person();


I also told you to create an instance of type Book, and pass the value of the $author variable to its constructor call. (this is very tough for me to understand but i tried it like below)
             
$title      = new Book();
 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
You created an instance of Book and assigned it to a variable named $title. I want you to assign it to a variable named $book, because a book is not a title. I also want you to pass the $author variable that you assigned in the line before to the constructor call.

I previously showed you how to pass a variable named $color to the method set_color(). What don't you understand about passing $author to the Book constructor call?
 
John Ken
Ranch Foreman
Posts: 342
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think i needed to code like below as per previous explanation example of color =red


$author = new Person();


$book = new book ();

$book__Constructcall  = $author;

but i am not sure whether above construct call is correct or not?
     
 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
No. You pass arguments to a constructor by supplying them between the parenthesis after the class name when you use the new keyword.

As I demonstrated here, I created an instance of Person by calling the constructor with a string literal for the author name, and I assigned it to a variable named $author. Then I created an instance of Book by calling the constructor with a string literal for the book title and the instance of Person for the book's author.

I used a variable $author to make the code a little bit clearer, but you don't necessarily need variables or literals when calling a method or constructor. You can also supply the return value of an expression directly:

This code does exactly the same thing as the previous code, except it doesn't save the author in a local variable first.

Do you understand the two code snippets? Do you understand how to call constructors? If you don't, tell us what you don't understand.
 
John Ken
Ranch Foreman
Posts: 342
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks for your clarification,now i understood better concept of this constructor with this example.

So please advise what to do next?





 
Stephan van Hulst
Saloon Keeper
Posts: 11882
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Now I want you to show me the Person and Book classes:

  • The Person class has a constructor that takes a string $name.
  • The Person  class has a getter that returns the name.
  • The Person class has a method __toString() that returns the name.


  • The Book class has a constructor that takes a string $title and a Person $author.
  • The Book class has a getter that returns the title, and a getter that returns the author.
  • The Book class has a method __toString() that formats its title and author in such a way that the example book I gave earlier returns "'Moby-Dick; or, The Whale' by Herman Melville"


  • Use type hints for the constructor parameters, instance fields and method return types.
  • No setters.
  • Format strings by using string expansion, not string concatenation.
  • I don't want to see any echo statements.
  • I REALLY don't want to see any class extending another class.
  •  
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    it again got tough however i tried like below:-

    person class:-

    <?php
    class Person {
     $name = new Person('ABC');

    public function get_name()

    {
    return $this->name;
    }

    pblic function __tostring()
    {
    return $this->name;
    }
     
     

    ?>

    *****
    Book Class:-

    <?php
    class Book {
     
    $book = new Book(
     'Moby-Dick; or, The Whale',
     new Person('Herman Melville')
    );
    public function get_name()

    {
    return $this->name;
    }

    pblic function __tostring()
    {
    return $this->name;
    }
     
     

    ?>

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
  • Why did you assign an instance of class Person to the $name field?
  • Why does your Book class contain a $book field?
  • Why does your Book class contain a get_name() method?
  • Why did you use underscores for your methods, instead of using camel case as I instructed in a previous post?

  • I want you to answer these questions, not post new code.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    1.because you asked The Person class has a constructor that takes a string $name.
    2. because The Book class has a constructor that takes a string $title and a Person $author
    3.because The Book class has a getter that returns the title, and a getter that returns the author.
    4.i don't understand what do you mean by camel case also i found some examples on google using this underscore methods any harm in using them?
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    also...i think..you said you want a person class with a method of $name// I understand and the Book class that takes a string $title and a Person $author and all classes should have a toostring method and you don't want any extending of class and you want me to call Person class in Book class it's impossible to call any parent class I a child class without Extending the Parent class// by using _tostring() function you can echo all methods without using the echo keyword I understand this but the extending must happen ):
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator

    John Ken wrote:1.because you asked The Person class has a constructor that takes a string $name.


    You didn't add a constructor to the Person class. You called a constructor that doesn't exist. Once again, see the PHP manual for how to declare a constructor in your class.

    2. because The Book class has a constructor that takes a string $title and a Person $author


    It doesn't. You didn't declare a constructor. You called a constructor that doesn't exist. And you assigned its return value to a field you don't need.

    3.because The Book class has a getter that returns the title, and a getter that returns the author.


    It doesn't. You added a getter that returns the book's name field, a field that doesn't exist by the way. I asked you to add getters that return a title and an author, not a name.

    4.i don't understand what do you mean by camel case also i found some examples on google using this underscore methods any harm in using them?


    I mean using getName() instead of get_name(). PHP does't really have consistent naming conventions, but in new code I believe it's convention to use camel case for method names and underscores for functions that are not part of a class.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    it is going very tough for me to correct it and getting desired results so could you please help on same?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    No.

    Explain what part you don't understand.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    i think it's corrected with respect to all points excpet 3rd. (as not sure how to correct it)

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You correctly added a constructor that accepts a name for a Person, and you correctly added a getter to get the name of a Person. Then why is it so difficult for you to add a constructor to Book that accepts a title and a an author? I literally told you to give it $title and $author parameters. Why did you ignore this requirement?

    Why are you initializing the $name field of the Person class with a Person instance? First of all, the $name field is supposed to hold a name, not a Person. Secondly, it's the constructor's responsibility to assign an initial value to this field.

    Answer all the questions I wrote in bold please.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    i thought that we need to declare constructor like this way as we declared in previous instance likewise we assigned instances in previous posts.anyway now i have asked constructor to assign value to it itself .hope below code meets these requirements now:-


     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    What does $this->name refer to in your Person constructor? What does $this->name refer to in your Book constructor? Why does Book have a $book field?

    Do you know what a field declared in a class is used for?
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    $this is a reserved variable name and can not be used for anything else.
    It specifically points you to the object your are currently working in. You have to use $this because you do not know what
    variable object will be assigned to.

    Book class has a $book field so hat we can give book name and it's author in to it.

    A field is a variable of any type that is declared directly in a class or struct.
    Fields are members of their containing type. A class or struct may have instance fields, static fields, or both. ... A private field that stores the data exposed by a public property is called a backing store or backing field.J
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You literally just copied answers from the C# programming guide and from Stack Overflow.

    I'm done helping you John, you're clearly incapable of independent thought. Good luck.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    you asked me what these refer to then i answered it based on best of resources available with me what was wrong in it?

    i know i am not that much technical expert like experts here but what can i do if i am like this but i have a passion to learn that is why i have been trying very hard to solve this tough problem despite answering all of tougher questions which i never expected to answer even though i never left the hope despite that i kept on answering those tough problems which came in the response of my main problem.but i never quit and kept on trying with best of my skills and knowledge if the end had to happen like this then why on the earth we have so called 'friendly green horn forums'
    which just insult others.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    I asked you the questions to make you think about it, and to gauge if you understand basic concepts. If you just parrot what other people say, there's no way to see if you understand or not.

    I already made it clear on several earlier occasions that I want you to put thought into this and not just blindly copy code or answers without understanding it. To me, passion for learning means asking intelligent questions about things you don't understand, not trying to appease me by blindly guessing at the code you think I want you to write.

    You don't need to be an expert to be able to do this, you just need to follow instructions carefully and ask targeted questions when you don't understand.

    "Friendly place for programming greenhorns" doesn't mean that we need to take it when somebody tries to pull the wool over our eyes by copying answers and thinking we won't notice.

    Most importantly, my patience with you has run out, and I don't think it's conducive to any programming career you might have that I continue to interact with you.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    but i tried my best ,accepted i copied but if i had no concrete answer then i had to copy it from available resources right,what would one have done if he had limited skills?

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11882
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Say you don't know. Ask questions.

    If you copy answers I'm going to assume that you understand and I'm going to move on to topics that you are not ready for, wasting both your and my time. And now I'm done wasting my own time, so I'm not going to pay attention to this topic anymore.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Ok,as I have very little knowledge on these tough things so I say i really don't know what these $this etc. refer to but   i think you would also have given same answer which i copied from internet isn't it?

    ..and if you leave this topic in middle then how would our main problem be solved soon now?

     
    Saloon Keeper
    Posts: 6380
    158
    Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
    • Mark post as helpful
    • send pies
    • Report post to moderator
    It won't, that much is obvious. You're not making progress learning PHP, which is a prerequisite to solving the problem.
     
    Rancher
    Posts: 142
    7
    • Mark post as helpful
    • send pies
    • Report post to moderator

    John Ken wrote:Ok,as I have very little knowledge on these tough things so I say i really don't know what these $this etc. refer to but   i think you would also have given same answer which i copied from internet isn't it?



    You didn't answer the question. You copied some random text from the internet. You cannot keep guessing. If you don't understand the basic concepts like variables and constructors you should first learn what they are.
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Ok,as I have very little knowledge on these tough things so I say i really don't know what these $this etc. refer to but   i think you would also have given same answer which i copied from internet isn't it?

    ..and if you leave this topic in middle then how would our main problem be solved soon now?

     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    i am trying my best to solve this problem my own but unable to solve it so far that is why i have been seeking experts help here but experts are not helping at all to solve it,instead of this they keep asking some tough technical questions even if i answer them then they say i am copying it from internet then in such a way how would we  ever be able to solve my main problem?
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    any updates by experts please?
     
    John Ken
    Ranch Foreman
    Posts: 342
    • Mark post as helpful
    • send pies
    • Report post to moderator
    php experts could you please advice further to solve this problem quickly?

    Thanks
     
    Tim Moores
    Saloon Keeper
    Posts: 6380
    158
    Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Hourly nagging isn't going to make anyone else to want to step in, so we seem to be done here. I'm closing this topic.
     
    Just the other day, I was thinking ... about this tiny ad:
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic