answersLogoWhite

0

How do you use class and object in php?

Updated: 9/21/2023
User Avatar

Wiki User

12y ago

Best Answer

I believe the best way is to show you an example:

class Car{

private $gas=0;

private __construct(){

$gas=500; //when you create an object your gas tank is automatically filled with 500 gas for a start

}

public function fill_the_tank($amount){

$gas=$amoung;

}

public function get_gas(){

return $gas;

}

}

$ferrari = new Car;

echo $ferrari->get_gas(); //prints out "500"

$ferrari->get_gas(); //prints out nothing, but also doesn't end up in error

$ferrari->fill_the_tank(100);

echo $ferrari->get_gas() //prints out "600"

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use class and object in php?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Basic Math

How do you create a PHP class?

To create a PHP class it is pretty simple, I will show you how to create a very basic class in an example below. <?php // the class class myBasicClass { // a function to add 2 numbers together and return the result function add($num1, $num2) { $result = $num1 + $num2; return $result; } } // load up the class $myBasicClass = new myBasicClass; // output the result echo $myBasicClass->add(5, 10); // outputs the result 15 ?>


What are the limitations of PHP?

Everything has limitations or disadvantages and PHP is no exception. The following are the limitations of PHP (so be WARNED !!) # PHP is NOT 100 % pure Object Oriented scripting language. But in near future PHP may support 100% object oriented scripting (PHP may imitate most of the syntax of Java language). PHP already imitates some features of Java language. (In future PHP language will imitate most features of Java language and Java programmers will love PHP. And PHP will have java keywords like class, extends, interface, implements, public, protected, private etc..). # PHP will NOT give the performance of "C" or "C++" language. Because it is scripting language and is interpreted it will be a bit slower than the optimized "C++" programs. For top performance, you should use "C++" and fast-CGI with database/webserver connection pooling and use C++ compiler optimizer "-O3" options. Zend optimizer in PHP 4 will speed up the performance of PHP and bring it very close to optimized "C" code . # But note a point that PHP was designed for very Rapid Web-Application Development tool. If it takes about 3 months to code a web application in C++, then using PHP you can develop the same web application in just 4 days!! And with zend optimizer, the speed of execution of PHP will be very close to that of equivalent C++ program!! Hence, there is really no advantage in using C/C++ for web development. PHP itself is written in 100% "C" language. On the other hand, PHP has lot of advantages and it's advantages outweigh it's limitations - # You can very rapidly develop web applications in PHP as compile and link is eliminated in PHP scripting language. # PHP applications are very stable and do not depend on the browser technologies unlike Javascript applications which depend on browsers. PHP will give you the freedom to select any server platform. The browser does not know that the HTML page is generated by PHP !! # PHP has excellent database conectivity to all SQL database servers. # PHP has partial support for Object oriented features # PHP has C++, Perl, Javascript like syntax features and has programs like 'ptags/ctags' to navigate the source code # PHP has Zend optimizer which speeds up the performance # PHP runs on all unixes, linux, Windows 95/NT/2000 and is more powerful than ASP, JSP and others. # PHP has a very large user base and developer base.Ref:http://www.linuxdocs.org/HOWTOs/PHP-HOWTO-14.html


Why we use javascript as we have a powerful scripting language php?

First of all, PHP is server side, Javascript is client side. You cannot detect mouse gestures or any clicks with PHP the same way you cannot read a file or modify it or process a form (unless you use AJAX of course). Also, although I love PHP, and it has useful extensions such as the GD library, PHP is not a very good language. Overall we use javascript and PHP because they are used for two completely different things.


What is syntax in php?

We can use php tags in different ways. <?php //php code to be written here ?> OR <? //php code ?> This tag will not work when we using editors such as macromedia dreamweaver. OR < script language="php"> //php code </script>


What character allows you to concatenate strings in PHP?

To concatenate strings in PHP, you use the . (dot) character. For example: $str = "String1" . "String2";

Related questions

Is PHP object oriented?

Yes - 'advanced' PHP programming uses Object Oriented Programming (OOP).


What class is the super class for ALL Java classes?

The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class


How do you implement breadcrumbs in PHP?

To Implements Breadcrumb In Front End You can use Bootstrap bread crumb Class. For Functionality URL can be traces Even using Javascript BOM(Browser Object Model) Properties.


What is the use of a question mark in php?

It is used to tell the sever to use the PHP parser. To begin php you must use <?php, and to end it, it is ?>.


How do you use PHP in HTML?

You can't use PHP in an HTML document, but you can use HTML in PHP script.


PHP case sensitivity refers to what?

PHP case sensitivity refers to that in coding it matters if the code is in capital letters. Case sensitivity specifically in PHP refers to variables, constants, array keys, class properties, and class constants. Some items in PHP that are not case sensitive are functions, class constructors, class methods, and keywords and constructs.


What is php plus plus?

PHP++ is an object-oriented version of the PHP programming language. ++ is used in programming to increment a variable by one so it means an improved version of PHP.


What is a sub class?

In object oriented programming approach subclass is derived from parent class. This term is generally used in concept called "Inheritance" Example [PHP] class A { //class A definition } class B extends A { //class B definition } In above example class A is parent class and class B is subclass/child class .


What is sub classes?

In object oriented programming approach subclass is derived from parent class. This term is generally used in concept called "Inheritance" Example [PHP] class A { //class A definition } class B extends A { //class B definition } In above example class A is parent class and class B is subclass/child class .


How do you escape database queries with PHP?

Use mysql_real_escape_string(), after you've connected to your database. If that function doesn't work, use mysql_escape_string() or addslashes(). Never leave your database input vulnerable to attack.


Is the word classes an object?

Classes is the plural of the word class. A class is not an object in the physical sense, it is an idea, a notion or concept. We typically use a class to define a classification, such as a class of people, a class of study or a class of object. In other words it defines a type.


How do you invoke method by using object of the class?

If you have and object with method described within its class you can use dot access operator, for instance:myObject.DoSomething();