12

i am beginner on php so now i try to learn object oriented i was goggling i got it some ideas but not clear concept.So i come there.Please any php guru give simple example of how to crate classes and how to call on other php page.

for example

i want two classes one is show name and second one is enter name.First class show name this name come from database and second class put name in database.

Index.php

<form action="checking.php" method="post">
      <input type="text" placeholder="Please enter name">
</form>
4
  • 2
    you do not need two class for that all you need is two method one for adding data and other is for fetching data from database.. Commented Dec 16, 2013 at 5:25
  • use setter and getter property for this. Commented Dec 16, 2013 at 5:26
  • @DipeshParmar yes you right i need one class thanks for suggestion. Commented Dec 16, 2013 at 6:37
  • Possible duplicate of What is a class in PHP? Commented Sep 28, 2017 at 11:45

2 Answers 2

14

The way you are calling a php page is good. That is from HTML.

What I think, you are getting this wrong. A class showName to get name from database and enterName to save in database. Well what I suggest that should be a function within one single class.

<?php
class Name
{
    public $name;
    public function showName()
    {
        /**
        Put your database code here to extract from database.
        **/
        return($this->name);
    }
    public function enterName($TName)
    {
        $this->name = $TName;
        /**
        Put your database code here.
        **/
    }
}
?>

In checking.php you can include:

<?php
    include_once("name_class.php");
    $name = $_POST['name'];   //add name attribute to input tag in HTML
    $myName = new Name();
    $myName->enterName($name); //to save in database/
    $name=$myName->showName(); //to retrieve from database. 
?>

This way you can achieve this, this is just an overview. It is much more than that.

Sign up to request clarification or add additional context in comments.

1 Comment

I think there's an error in your code. You can't put the $ after a $this-> because it will throw a PHP exception. See line this->$name = $TName;
4

You have to create a class person and two methods..

class Person{
    public $name;
        public function showName()
        {
             echo $this->name;
        }

        public function enterName()
        {
             //insert name into database
        }
}

2 Comments

You should not "echo" from within a method. Just "return" and let the user of this class decide what he want to do with the given data.
@MischaBehrend, return is good. But this method is not getName(). For getting/setting getter/setter will be good.. Anyway thanks for your interest in this answer after two years though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.